Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

No memory leak with free of base class

Posted on 2002-05-12
6
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:Hagen040798
ID: 7004333
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
ID: 7004467
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
ID: 7004508
Yes, if we assume as underlaying Memorymanager the Borland Standard one.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:koger
ID: 7010451
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 400 total points
ID: 7011780
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
ID: 7023674
Now that's an impressive answer :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

609 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