Re: Multiple destructors?



"Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx> schreef in bericht
news:43c2dfd8$0$11065$e4fe514c@xxxxxxxxxxxxxxxxx
> Hello all,
>
> Has anyone here ever written a class with more than one destructor?
> I was just toying with some hobby code where objects are used to model
> a filesystem, and wondering how I might add the feature of deleting
> files and directories[0]. There are filesystem, file, and directory
> objects, and it seemed to me that destroying a file object might be a
> very natural way to also delete the corresponding file.
>
> File and directory objects are owned by their parents and freeing a
> filesystem object obviously shouldn't format its backing storage. So
> "the" destructor is taken, as it were. Then it occurred to me that
> Delphi doesn't stop me from having two destructors. Does anyone have
> thoughts on that? (I'm looking to Marco in particular, as Google
> revealed that FreePascal _will_ stop me from having two destructors.)
>
> I realise that it is very convenient to say the least to have only
> the standard Destroy destructor. But does it go beyond convenience?
>
> It is possible to have detached file and directory objects, that do
> not represent an allocated object in the actual filesystem. Should I
> take this as an argument against this way of deleting files?
>
> Groetjes,
> Maarten Wiltink
>

I remember an application where I had called destructors in a derived
class of Tlist DONE instead of DESTROY.
Destroy would just destroy the list, but leave its elements intact. Done
would first free all elements and then free the list. But at one place I
messed it up because I hadn't realized that Destroy has a special meaning.
For it is the destructor called by Free. And if you are in the habit of
calling Free you may have memory leaks when there isn't a Destroy in the
class.

Tom


.



Relevant Pages

  • Re: Multiple destructors?
    ... and it seemed to me that destroying a file object might be a ... > Destroy would just destroy the list, ... For it is the destructor called by Free. ... Actually, on checking, all virtual methods in TObject are in the ...
    (comp.lang.pascal.delphi.misc)
  • Multiple destructors?
    ... Has anyone here ever written a class with more than one destructor? ... I was just toying with some hobby code where objects are used to model ... There are filesystem, file, and directory ... It is possible to have detached file and directory objects, ...
    (comp.lang.pascal.delphi.misc)
  • Re: Class Destructor doesnt destroy?
    ... calling the destructor doesn't completely destroy it. ... your program exhibits undefined behavior under C++ ... or automatic storage duration and if T has a non-trivial ...
    (microsoft.public.vc.language)
  • Re: Class Destructor doesnt destroy?
    ... the destructor won't be called ... Microsoft MVP, MCSD ... operator delete, but if I instantiate it without operator new, I don't ... seem to be able to destroy it until it goes out of scope - manually ...
    (microsoft.public.vc.language)
  • Re: PreTranslateMessage gets called wrongly (after view destroyed)
    ... leave the destructor to actual memory deallocation of items that need it? ... deleting the controls only sends messages asking them to ... destroy themselves, so that should be done within the MFC destruction ... leaving the CPP destruction process to actually release memory as ...
    (microsoft.public.vc.mfc.docview)