[fpc-pascal] Usage of TInterfacedObject. Automatic release?

Luiz Americo Pereira Camara pascalive at bol.com.br
Sat May 26 21:57:03 CEST 2007

Cesar Romero wrote:
> Luiz Americo,
>> Anyway, calling manually _Release avoid the leak and i will stay with 
>> it for now (I hope did not break anything).
> It can work for that case, but I think that is not a good ideia.
> If you are using a TInterfacedObject descendant, it will call the 
> _Release when out of context, so in that especific case seems to never 
> go out of context, and then you _realease call works, but in other 
> place it can go out of context before, and the you will have others AV.
> - As Joao suggest, the best way is create a weak reference, where when 
> 2 objects need to reference each other one should use Pointer to hold 
> one reference
> - Avoid circular reference

The problem here is that i'm using a third party design. And the code 
complexity allied with my inexperience with interfaces/COM makes the 
chance of  breaking things big.

Anyway i did not find, with my limitations, any circular references 
between interfaces. Exists a circular reference but is between an 
Interface (IVTDragManager) and a TVirtualTreeView(TCustomControl) 
already used as a weak reference (TObject).

The design is more or less the following:

TBaseVirtualTree holds a reference to a IVTDragManager.
TVTDragManager (IVTDragManager) holds a reference to TBaseVirtualTree 
stored in a TObject field
TVTDragManager also holds reference for IDataObject (TVTDataObject) and 
IDropTargetHelper (returned by a win32 call)
TVTDataObject holds a reference to TBaseVirtualTree as a TObject

There are two circular references:
TVTDragManager <-> TBaseVirtualTree
TBaseVirtualTree <-> TVTDragManager > TVTDataObject > TBaseVirtualTree

if someone is interested. the code is here: 
> - Create a Finalization method that will set one of the references to 
> "nil"
Its already done. But the problem is that this finalization code is  not 
called (is inside the Interface Destroy). And setting the interface 
reference to nil inside TBaseVirtualTree.Destroy does not help.



More information about the fpc-pascal mailing list