[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: The great destroy mystery.
From:       Thiago Macieira <thiagom () wanadoo ! fr>
Date:       2003-08-08 0:36:03
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


Gary Cramblitt wrote:
>In my object destructor, do I need to explicitly destroy object pointers
>declared as member variables and initialized with _new_ or not? 

Yes. For everything that you new, you must delete and exactly only once.

>I can't 
>find any consistent info on this. The C++ FAQ says that such objects are
>"automagically" destroyed when the parent object is destroyed.

It was not referring to objects allocated in the heap with new.

>But the FAQ says that calling delete more than once on the same object
>won't hurt.  "Thinking in C++" is strangely mute about "automagic"
>destruction.

Deleting twice the same object is definetely wrong. And will cause trouble 
most of the times. That's why setting it to 0 is a good recommendation if the 
pointer is still going to be accessed.

>There was a thread in this list about a month ago that asked the same
>question, but once again, there were conflicting answers.

So here's the answer: for every new, there must be exactly one delete. It's 
like malloc: there must be one free.

-- 
  Thiago Macieira  -  Registered Linux user #65028
   thiagom@mail.com           
    ICQ UIN: 1967141   PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

[Attachment #5 (application/pgp-signature)]

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic