Re: Multiple destructors?



"Tom de Neef" <tdeneef@xxxxxxxx> wrote in message
news:43c3ba9d$0$11076$e4fe514c@xxxxxxxxxxxxxxxxx
> "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx> schreef in bericht
> news:43c2dfd8$0$11065$e4fe514c@xxxxxxxxxxxxxxxxx

>> 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.
>> [...]

> 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.

That's like TObjectList, which may or may not own the objects in it,
only different. The similarity to ownership schemes had not occurred
to me.

TObjectList has a property OwnsObjects. I could have a property in my
file objects, but it would just be waiting to be used as an implicit
parameter to Destroy. I don't know what's worse: static parameters or
parameters on destructors.


> 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.

Every class should have a Destroy destructor. I noticed today that it
even has its own slot in the negative offset section of the VMT.
Actually, on checking, all virtual methods in TObject are in the
negative offsets. Simply a matter of where it was decided for them
that they should start counting, apparently.

Groetjes,
Maarten Wiltink


.



Relevant Pages

  • Re: Dopey Delphi
    ... Destroy, ... The destructor releases the memory. ... FreeAndNil which can hide some nasty bugs). ... there is quite a lot of that in Delphi by now. ...
    (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: Multiple destructors?
    ... There are filesystem, file, and directory ... > "the" destructor is taken, ... > It is possible to have detached file and directory objects, ... class of Tlist DONE instead of DESTROY. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Text der Zwischenablage bei Änderung auslesen
    ... procedure WMDrawClipboard(var Message: TMessage); ... procedure TForm1.FormCreate(Sender: TObject); ... NextViewer:= SetClipboardViewer; ... Im Destructor würde ich mit ChangeClipbordChain ...
    (de.comp.lang.delphi.misc)