why aren't descendants of TInterfacedObject garbage collected?
Asked Answered
A

3

4

i have a class based on TInterfacedObject. i add it to TTreeNode's Data property.

TFacilityTreeItem=class(TInterfacedObject)
private
  m_guidItem:TGUID;
  m_SomeOtherNode:TTreeNode;
public
end;

i create many instances of this object & had assumed that because they're reference counted, i shouldn't need to Free them. that'd be handy.

however, when checking this, i turned on ReportMemoryLeaksOnShutdown and found they're not being freed after all.

these objects are being created in a frame that's placed on the main form. in the main form's FormClose, i clear the tree nodes such that every object should be freed.

what's happening?

thank you for your help!

Astragalus answered 27/1, 2009 at 19:49 Comment(0)
A
16

TInterfacedObject itself is not reference counted, only interfaces are. You can implement interfaces using TInterfacedObject which basically saves you the effort of implementing the reference counting methods yourself. Unfortunately it still will not work in your case: The compiler does not know that you are assigning interfaces to the TTreeNode.Data property since it is not declared as an interface but as a pointer. So all kinds of weird things will happen:

MyInt := TFacilityTreeItem.Create; // ref count = 1
// Node.Data := MyInt; // won't compile
Node.Data := pointer(MyInt); // no interface assignment, ref count stays 1
...
end; // ref count reaches 0, your object gets freed

As soon as you try to access your object through the .Data property, you will get an access violation.

So, don't bother with interfaces in this case, you could get it to work, but it will be much more effort than it is worth.

Ankney answered 27/1, 2009 at 20:15 Comment(1)
thank you for your answer; i'll use TObject instead. learned something new!Astragalus
E
3

You should declare the Field/Variable as Interface

IFacilityTreeItem = IInterface
end;

TFacilityTreeItem=class(TInterfacedObject, IFacilityTreeItem)
private
  m_guidItem:TGUID;
  m_SomeOtherNode:TTreeNode;
end;

var
  Item: IFacilityTreeItem; // Variable as Interface
begin
  Item:= TFacilityTreeItem.Create;
...
end;

To access your fields, you should declare properties in IFacilityTreeItem Interface, with Getters and Setters.

Ewold answered 27/1, 2009 at 21:28 Comment(2)
thank you for your answer. i'm not happy to make the getters/setters so i suppose i'll make it descend from TObject instead.Astragalus
@Cesar Romero: Please edit your code, it would never compile (interfaces can not have data members, nor anything private).Undaunted
D
2

As dummzeuch said, you can get this to work with interfaces, but it takes some more code since the Data property of a TTreeNode is a pointer. For anyone wondering how to do that, this link has an example of how to do it for TListItem (it's pretty much the same for TTreeNode). You may also find it useful to read the section about interfaces and subsequent section about reference counting on that page.

Dorsiventral answered 17/2, 2009 at 6:55 Comment(1)
+1 for the tip to read the section on interfaces in "Delphi in a Nutshell". It is short but to the point, and there is in general little information to be had on interfaces in Delphi. Quite some misconceptions are around, too.Undaunted

© 2022 - 2024 — McMap. All rights reserved.