Solved

Is it ok to have a class with no parent?

Posted on 1998-09-16
9
241 Views
Last Modified: 2013-11-23
I am trying to track down some memory leaks in my project using Memory Sleuth. I am wondering if there is a problem declaring (formless) classes having no parent, thereby not calling an inherited Create and Destroy in the constructor and destructor respectively?
0
Comment
Question by:tomcorcoran
9 Comments
 

Expert Comment

by:ShadowHawk071998
ID: 1339960
First of all, when you make an object that doesn't inherit from anyone, Delphi automatically makes Tobject it's ancestor. So you can ALWAYS call inherited destroy (Actually calling Free is recommended, because it checks for nil pointers before destroying).  So that will solve your problem. But I don't think the memory leaks comes from that!
0
 

Author Comment

by:tomcorcoran
ID: 1339961
Shadow,

Thanks for the answer.

" So you can  ALWAYS call inherited destroy (Actually calling Free is recommended, because it checks for nil pointers before destroying)."

I always call Free, however what you are saying doesn't make much sense, I was asking about "inherited Destroy" the  last line normally in a destructor, you wouldn't use Free in this case. You say I can always call it, I am trying to find out is it cool not to in this instance (similiarly with inheritd create)..

Any other feedback anyone?

Thanks, Tom.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1339962
You can do that for decendent of TComponent only but not TClass in general. This is because all component has an owner, and the owner is reponsible to free the owned component before the owner itself is destroyed.
p/s: I hope I understand your question correctly: when you say parent you mean owner?

0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1339963
You only have to explicitly call the destructor (or free or whatever) of the base class if it allocated memory. TObject does not do this., it only implements methods and you thus cannot leak by not calling it's destructor.

Where do you expect your leak is ? What do you do in your class ?
It is generally bad programming practice to use something like new or malloc in constructors because it can cause problems. If you do not do this the classes should destroy ok unless you are dropping pointers or not calling inherited destructors.

A common error is not declaring a virtual destructor in your class or actually the class of which type you are freeing. If you e.g. derive TMyObject from TObject and then access TMyObject via a pointer of type TObject simply freeing the TObject will result in a leak since the compiler and the app will not know that it is actually a TMyObject that has to be freed ! If you do have virtual functions that cause VTable entries to be made the code will determine the type from the VTable and it should free correctly. It is however a good Idea to always free something cast to the same type as it was created.

I do not know what you did in your code so forgive if this is completely off track ...

Hope that helped !
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Expert Comment

by:ugo072998
ID: 1339964
Hi tomcorcoran,
I don't know what's inside your classes, but if use descendent of TComponent instead of TObject (implicit also if don't declare anything) the memory management is always correct.
Delphi Help -> "Calling Free for a component, will call Free for all components that it owns, that is, all components in its component list."
So it shouldn't be so difficult to change yoru classes declaration inheriting from TComponent.
You just need to add AOwner in your create method.
e.g. declaring two kind of classes like follow :


TMyContained = class(TComponent)
private
   name : string;
   list : TStringList;
   procedure DoSomethingInside;
   . . .
public
   constructor Create(AOwner : TComponent);
   destructor destroy; override;
end;

TMyContainer = class(TComponent)
private
    MyContained : TMyContained;
public
   constructor Create(AOwner : TComponent);
end;

implementation

// TMyContainer
constructor TMyContainer.Create(AOwner : TComponent);
begin
    inherited create(AOwner);
    MyContained := TMyContained.create(self);
end;

// TMyContained
constructor TMyContained.Create(AOwner : TComponent);
begin
    inherited create(AOwner);
    list := TStringList.create;
end;

destructor TMyContained.free;
begin
    // if you have objects in the list you must make a loop to free any of them
   //  before freeing the list
    list.free;
    inherited destroy;
end;

This way you'll discard any allocated memory. Note that because of TStringList doesn' descend from TComponent you need to free it by code.
So if you want to declare something directly descending from TObject you must manage by yourself free and destroy methods.
Sometimes, like using TStringList, it's necessary.
I hope tehere's something helpful for you .
Giovanni.
0
 

Author Comment

by:tomcorcoran
ID: 1339965
Giovanni, thanks for the reply, I have many classes liker this, this is not what I am asking however, JimBob pretty much answered it, saying it's cool not to call the inherited create and destroy when TObject is the parent (explictly or implicitly). JimBob, let's put this one down, most a dummy answer and I'll grade. Tom.
0
 

Expert Comment

by:ugo072998
ID: 1339966
Hi Tom,
you wrote about 'Memory Sleuth', is it a good tool for memory management ?
I've been on FILEZ looking for it but I couldn't find anything.
I'm looking for a good tool so please tell me more about it.

burzox@tin.it
0
 

Author Comment

by:tomcorcoran
ID: 1339967
Hey, check out UNDU - http://www.undu.com/Articles/980807c.html, I find Memory Sleuth the best. Tom
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1339968
Hi Tom

I had actually commented in your other question, so only found your comment here by coincidence!  :-)

Anyway, if you're happy with my comment on the other question and are still happy for it as an answer, then here it is.

Cheers,
JB
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

9 Experts available now in Live!

Get 1:1 Help Now