Hi all

I'm developing a program with Visual C++ a I'm wondering if I can write this sentence in Visual C++: 'delete this'. The main objective of this sentence is to the delete the object that I'm in, does anyone know if this can cause some errors?

Thanks

Re: delete this by Marius

Marius
Mon Mar 01 11:06:23 CST 2004

Hi,

"Vicent Soler" <anonymous@discussions.microsoft.com> wrote in message
news:0D8AFB3E-CE40-470D-8F9A-3B0954AAAB51@microsoft.com...
> Hi all,
>
> I'm developing a program with Visual C++ a I'm wondering if I can write
this sentence in Visual C++: 'delete this'. >The main objective of this
sentence is to the delete the object that I'm in, does anyone know if this
can cause some >errors??

You can do it. However, you have to make sure you do it right... for
example, if your object is derived from another object, make sure your
destructors are virtual... also make sure that only one "delete this" takes
place and not two [for instance, a delete this could exist in the base
implementation, etc.]

Marius




Re: delete this by Bonj

Bonj
Mon Mar 01 13:53:32 CST 2004

also could cause issue if member functions / data were accessed after
'delete this'

??


"Marius Prisecaru" <prisasm@h0tmail.remove-this-n-make-zero-o.com> wrote in
message news:ujI9556$DHA.1456@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> "Vicent Soler" <anonymous@discussions.microsoft.com> wrote in message
> news:0D8AFB3E-CE40-470D-8F9A-3B0954AAAB51@microsoft.com...
> > Hi all,
> >
> > I'm developing a program with Visual C++ a I'm wondering if I can write
> this sentence in Visual C++: 'delete this'. >The main objective of this
> sentence is to the delete the object that I'm in, does anyone know if this
> can cause some >errors??
>
> You can do it. However, you have to make sure you do it right... for
> example, if your object is derived from another object, make sure your
> destructors are virtual... also make sure that only one "delete this"
takes
> place and not two [for instance, a delete this could exist in the base
> implementation, etc.]
>
> Marius
>
>
>



Re: delete this by Marius

Marius
Mon Mar 01 15:42:29 CST 2004


"Bonj" <a@b.com> wrote in message
news:eo$#bb8$DHA.2180@TK2MSFTNGP09.phx.gbl...
> also could cause issue if member functions / data were accessed after
> 'delete this'
>
> ??

Yes... That's why delete this should be the 'last' thing called and at a
point where you know the object(s) are not needed anymore... once the delete
this has been called, all the virtual destructors will be called..

In fact, I had a discussion about this some while ago in some other forum
and I posted there a test-case for a few scenarios.

Here are the results:

class A
{
public:
A() {};
virtual ~A() { cout << "Destructor of A called...\n"; };
virtual Method() {};
};

class B : public A
{
public:
B() {};
virtual ~B() { cout << "Destructor of B called...\n"; }
virtual Method() {delete this;};
};

class C : public B
{
public:
C() {};
virtual ~C() { cout << "Destructor of C called...\n"; }
virtual Method() {B::Method();}
};

then,

A* TheObject = new C;
TheObject->Method();

Output is:

Destructor of C called...
Destructor of B called...
Destructor of A called...
Press any key to continue

Removing the B::Method from the Method's implementation in class C, yields
that no destructors are called at all.

Doing a "delete this" in the Method implementation in class C [w/ nothing
written for the implementation of Method in A and B] yields:

Destructor of C called...
Destructor of B called...
Destructor of A called...
Press any key to continue

Marius




Re: delete this by Doug

Doug
Mon Mar 01 15:46:40 CST 2004

Bonj wrote:

>also could cause issue if member functions / data were accessed after
>'delete this'

In addition to what Marius has told you, you must also consider what happens
along the return path. For example, suppose you have a set of functions:

void f1()
{
f2();
...
}

void f2()
{
f3();
...
}

void f3()
{
delete this;
...
}

None of the "..." parts can be allowed to access non-static members.

--
Doug Harrison
Microsoft MVP - Visual C++

Re: delete this by David

David
Mon Mar 01 16:58:02 CST 2004


"Vicent Soler" <anonymous@discussions.microsoft.com> wrote in
message news:0D8AFB3E-CE40-470D-8F9A-3B0954AAAB51@microsoft.com...

> I'm developing a program with Visual C++ a I'm wondering if I can
write this sentence in Visual C++: 'delete this'. The main objective
of this sentence is to the delete the object that I'm in, does
anyone know if this can cause some errors??

As others have said, use it carefully. But there are some cases
where it is very useful indeed. A useful way of getting rid of
(eg) a modeless dialogue splash panel (derived from CDialog) can
involve something like:

void CDgSplashPanel::PostNcDestroy()
{
CDialog::PostNcDestroy();
delete this;
// Don't try anything else here!
return;
}

[ To quote Microsoft on the PostNcDestroy method:

Called by the default OnNcDestroy member function after the window
has been destroyed.
virtual void PostNcDestroy( );Remarks
Derived classes can use this function for custom cleanup such as the
deletion of the this pointer.]

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm








Re: delete this by Doug

Doug
Mon Mar 01 17:28:59 CST 2004

David Webber wrote:

>As others have said, use it carefully. But there are some cases
>where it is very useful indeed. A useful way of getting rid of
>(eg) a modeless dialogue splash panel (derived from CDialog) can
>involve something like:
>
>void CDgSplashPanel::PostNcDestroy()
>{
> CDialog::PostNcDestroy();
> delete this;
> // Don't try anything else here!
> return;
>}

NB: If you "delete this", you should call the base class PostNcDestroy only
if you know exactly what it does. In particular, it would be bad to call it
if it also does "delete this".

--
Doug Harrison
Microsoft MVP - Visual C++

Re: delete this by Simon

Simon
Tue Mar 02 03:45:10 CST 2004

Yeah, I suppose you could go some way towards ameliorating this by having a
static flag member of the class which is cleared just before calling the
base class CDialog::PostNcDestroy() and set by the derived destructor. Then,
if it has been set, the base class does a "delete this" and so you don't
need to. Like this:

class CDgSplashPanel: public CBaseClass
{
typedef CBaseClass inherited;
...
private:
static s_bDeleted;
}

static CDgSplashPanel::s_bBaseDoesDeleteThis;

void CDgSplashPanel::PostNcDestroy()
{
// forget about concurrency issues for now...
s_bDeleted = false;
inherited::PostNCDestroy();
if (!s_bDeleted) delete this;
ASSERT(s_bDeleted);
}

void CDgSplashPanel::~CDgSplashPanel()
{
s_b_Deleted = true;
}

"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:ghh740l2fs2q37dm53q0uq7gfd1khv8moo@4ax.com...
> David Webber wrote:
>
> >As others have said, use it carefully. But there are some cases
> >where it is very useful indeed. A useful way of getting rid of
> >(eg) a modeless dialogue splash panel (derived from CDialog) can
> >involve something like:
> >
> >void CDgSplashPanel::PostNcDestroy()
> >{
> > CDialog::PostNcDestroy();
> > delete this;
> > // Don't try anything else here!
> > return;
> >}
>
> NB: If you "delete this", you should call the base class PostNcDestroy
only
> if you know exactly what it does. In particular, it would be bad to call
it
> if it also does "delete this".
>
> --
> Doug Harrison
> Microsoft MVP - Visual C++