Solved

No memory leak with free of base class

Posted on 2002-05-12
6
192 Views
Last Modified: 2010-04-04
Hi
Can somebody explain why there isn't any memory leak here ?

type ttest=class(tcomponent)
private
    fa: string;
published
    property a: string read fa write fa;
end;


procedure TFrmMain.Button1Click(Sender: TObject);
var
test: ttest;
begin
while true do
begin
    test := ttest.create(nil);
    test.a := 'errror!';
    tcomponent(test).free;   //<--- no mem. leak here?!?
end;
0
Comment
Question by:koger
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:Hagen040798
Comment Utility
TObject.Free calls internal TObject.Destroy; and .Destroy is virtual. A destructor calls .FreeInstance; and this Method use some RTTI member to cleanup the Instance, they Interfaces, reference counted Fields, like Strings, dyn. Arrays, and finally calls Freemem(Self, Self.InstanceSize) to free the Instance it self.
0
 
LVL 11

Expert Comment

by:robert_marquardt
Comment Utility
and Self.InstanceSize is ignored because the size of the allocated block is stored inside the allocated block.
0
 
LVL 2

Expert Comment

by:Hagen040798
Comment Utility
Yes, if we assume as underlaying Memorymanager the Borland Standard one.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:koger
Comment Utility
Could you describe a little more.
Free is called to TComponent's, but there is no string propery in TComponent. How does TComponent know how to clear the property and free it?
0
 
LVL 2

Accepted Solution

by:
Hagen040798 earned 100 total points
Comment Utility
First. Each memoryblock allocated by the standard Memorymanager contains before the memoryblock an Integer that storres the allocated size of the Block. This size might be slightly greater than the requested memorysize. On deallocation the MM decrement the Pointer by 4 and get so this Value (called SizeFlags).
Second. For each Class the Compiler store into the codesegment an Record that describe the Class. This record contains the VMT = Virtual Method Table, the DMT = Dynamical Method Table, the Interfacetable of the supported Interfaces of the class, some classmember like a pointers or datas for the ParentClass, ClassName,  Instancesize, Properties and published methods, and to the TypeInfo for the Class. This TypeInfo IS the RTTI = Run Time Type Information to every Type that we use. After all the Compiler generates a big Tree of Classinformations linked to the second big Tree of Typeinformations. Now, on destruction of any Instance we can easily get all needed Information to clear any Instance-Fields that pointed to Interfaces or referencecounted Types like Strings, dynm. Arrays, Variants and other this.

You should take a look, suppose You can read assembler, into System.pas and study such procedures as .FreeInstance or _FinalizeRecord;

Hagen
0
 

Author Comment

by:koger
Comment Utility
Now that's an impressive answer :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now