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

List:       kde-devel
Subject:    Re: The great destroy mystery.
From:       Roger Larsson <roger.larsson () norran ! net>
Date:       2003-08-08 1:06:48
[Download RAW message or body]

On Friday 08 August 2003 02.04, 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?  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.

Are you sure that it says "Object *pointer"? It can't do that!
(Exception: the QObject case)
You might have a pointer to a shared object that should remain.
If it is a "Object object" then it will be automatically destroyed.

> Elsewhere 
> in the same FAQ, it says that one _must_ call delete for every new. ??

Objects you create and give away should not always be deleted by you.
Objects that you receive from others might need to be deleted by you.
This is what makes it hard - use java :-)

> And if I explicitly destroy an object in my destructor, do I risk
> destroying an object that has already been destroyed, thereby causing
> havoc?  "Thinking in C++" has the following statement with regard to
> _delete_:
>
> "If the pointer you're deleting is zero, nothing will happen. For this
> reason, people often recommend setting a pointer to zero immediately
> after you delete it, to prevent deleting it twice. Deleting an object
> more than once is definitely a bad thing to do, and will cause problems."
>

You may check with assert() to for pointers that you are sure to be 
existing (or deleted already).

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

You have to be pretty sure that it still is the same object!
In a threaded program where another thread needs memory it might
have got it after first delete. If you later delete it as having the first 
objects class - _really_ strange things might happen...

>
> There was a thread in this list about a month ago that asked the same
> question, but once again, there were conflicting answers.
>
> As Denzel Washington said in _The Philadelphia Story_, can someone
> please explain this to me like I were a two-year old?  Please?
>
> Here's some sample code fragments to help the discussion along.
>
> kspeak_part.h
> -------------
> class kspeakPart : public KParts::ReadOnlyPart
> {
>     Q_OBJECT
> public:
>     /**
>      * Default constructor
>      */
>     kspeakPart(QWidget *parentWidget, const char *widgetName,
>                     QObject *parent, const char *name);

Remember to initialize m_process in constructor. 
 kspeakPart::kspeakPart(....) : m_process(NULL) { ... }
It generates better code than an assignment.

>
> kspeakPart::~kspeakPart()
> {
>     // To delete or not to delete?
Delete - but that will kill the process!
>     delete m_process;
OK, remember delete NULL == NO Operation

You could write it like this, if you want to _always_ set
a deleted pointer to NULL:
	delete m_process; m_process = NULL;

/RogerL

-- 
Roger Larsson
Skellefteċ
Sweden
 
>> 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