Anyone knows what kind of situation usually causes an exception in System.__IntfClear?

Hello!

I am getting an AV in __IntfClear in System.pas .
I am assuming this means something is nil but the very same code works perfectly in some cases whereas I get exception in others. All the classes inherit from the same base class and they have essentially the same code, i.e. the same methods are overridden in the same way etc. you get the idea.

So I am wondering if anyone has an idea of what may be causing this especially because the functionality isn’t affected, only the destroying.

Any ideas?

Thank you!

I think @vincent has spoken in the past about sometimes having issues with interfaces freeing themselves (at termination?) in unexpected orders (… or something like that).

What’s even more interesting is this: say that there are 5 services affected by this problem. 3 of these will not have the issue if one of the other 2 (which seem to consistently have the issue) have the issue.
Otherwise, any of the 5 will have that issue.
I have no idea what’s happening.

An av on _intfclear means the reference counted object has been freed already.

These issues can be very hard to find - a code smell for this is if you manually call _Addref or _Release anywhere.

I would suggest that you keep the lifetime of references as short as possible - when you no longer need a reference, nil it out. Do this religeously.

Also, when working with reference counted objects that the current class or method doesn’t own, take a local reference and work on that. e.g

procedure TMyClass.DoSomething;
var
  LMyService  : IMyService;
begin
  LMyService := MyExternalService; //attempt to extend the lifetime of the object
  //it may already be nil so still need to check
  if  LMyService  <> nil then
    LMySerivce.SayHello;
end;

Doing this ensures the rug isn’t pulled out from under you.

1 Like

@Andrea_Raimondi … You didn’t mention which compiler you were using.
I wonder if anything here is relevant to your problem ?

A little off topic, but quite interesting

CodeSiteEx (ackersoft.com)

choose overview and then compiler magic.

Doesn’t 10.4 changes break the magic that is used?