delphi - Why do interface implementations based on TComponent leak memory? -
This Delphi code will show a memory leak for an example of TMyImplementation:
Program LeakTest; Uses classes; Type MyInterface = interface end; TMyImplementation = Class (TComponent, MyInterface) end; TMyContainer = Class (TObject) Private FInt: MyInterface; Public Property Implements: Read MyInterface FIST to FInt; End; Var C: TMyContainer; Starting ReportMemory Lexon Shutdown: = True; C: = TMyContainer.Create; C.Impl: = TMyImplementation.Create (zero); C.Free; End.
If TComponent is replaced by TInterfacedObject and the constructor has been replaced to make it, then the leak disappears. What's different with TComponent?
Thanks a lot for the answer briefly: It's easy, but wrong to say, "If you are using the interface, then their reference is counted and therefore they are free for you." In fact any class that implements the interface can break this rule. (And no compiler gesture or warning will be shown.)
difference in implementation
-
TComponent._Release
Not Free Your Example -
TInterfacedObject._Release
Free is your example.
Perhaps somebody has spoken but I have this on my mind that TComponent
is not used as a reference to the object The way we use the normal interface
Implementation of TComponent._Release
function TComponent._Release: integer; If FVCLComObject = Zero then results: = -1 / -1 indicates that no reference count is taking other place results: = IVCLComObject (FVCLComObject) ._ release; End;
Comments
Post a Comment