473,325 Members | 2,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

"delete this;" Is this safe?

Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?

The full article is here :
http://www.flipcode.com/articles/art...nplugins.shtml

Thanks,

solosnake


Jul 22 '05 #1
10 2117
On Sat, 10 Jan 2004 13:19:55 +0000, solosnake wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?


Yes, it is. Just make sure you don't touch any member afterwards. Yhis
includes any use of a data member and calling any non static function.

Sometimes this technique is necessary. I used to use it pretty often. I
haven't used it for some time now, I like to think that my design skills
have gotten better. Still there are valid uses.

HTH,
M4

Jul 22 '05 #2
"solosnake" <solosnake@solosnake._remove_this_.fsnet.co.uk> wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?


Can you guarantee that no CConsole object will ever be allocated on the
stack?
Jul 22 '05 #3

"solosnake" <solosnake@solosnake._remove_this_.fsnet.co.uk> wrote in message
news:bt**********@news5.svr.pol.co.uk...
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?

The full article is here :
http://www.flipcode.com/articles/art...nplugins.shtml

Thanks,

solosnake


Yes it is but I would call it poor design.
As a matter of principle it is desirable that only the class that created an
object should delete it (or should arrange for its deletion by putting it in
a handle class such as auto_ptr) - this allows scope for custom object
memory allocation without changing 'client' classes.
Jul 22 '05 #4
"Nick Hounsome" <nh***@blueyonder.co.uk> wrote:
"solosnake" <solosnake@solosnake._remove_this_.fsnet.co.uk> wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?

The full article is here :
http://www.flipcode.com/articles/art...nplugins.shtml

Thanks,

solosnake


Yes it is but I would call it poor design.
As a matter of principle it is desirable that only the class that created an
object should delete it (or should arrange for its deletion by putting it in
a handle class such as auto_ptr) - this allows scope for custom object
memory allocation without changing 'client' classes.


To put this another way, your code should make it explicit what object
is in charge of deleting what other objects. The code example muddies
the waters because it puts the 'delete' in CConsole, but CConsole isn't
really in charge of deleting itself, whatever object calling Release is
the one really in charge of the destruction...
Jul 22 '05 #5
solosnake wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?


Yup. The most common place you'll see this (at least, I haven't seen it
anywhere else much) is when programming with Microsoft COM. COM coclasses
use reference counting to manage their lifetime, which consists of two
functions, one called AddRef(), which increases the reference count, and
Release(), which decreases the reference count and when it reaches zero much
destroy the coclass. Implementations using C++ classes will usually
implement Release() using 'delete this;'. | As was pointed out this is only
not safe when the object is created on the stack, which is not possible with
COM objects.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge

Jul 22 '05 #6
Daniel T. wrote:
....

To put this another way, your code should make it explicit what object
is in charge of deleting what other objects. The code example muddies
the waters because it puts the 'delete' in CConsole, but CConsole isn't
really in charge of deleting itself, whatever object calling Release is
the one really in charge of the destruction...


I thought that the whole purpose of reference counting was that you
removed the "explicit" nature of what deletes what.

It's much more flexible to design a system where an object is deleted
when it is no longer "referenced". Of course, you have the issue of
circular referencing but *that* is a design problem.

Jul 22 '05 #7
"Daniel T." <po********@eathlink.net> wrote in message news:<po******************************@news03.east .earthlink.net>...
"solosnake" <solosnake@solosnake._remove_this_.fsnet.co.uk> wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?


Can you guarantee that no CConsole object will ever be allocated on the
stack?


It also can't be an element in array, ie., can't have:

a = new CConsole()

and then use Release() on a member of the array. Ie.,
dangerous to say:

a[0].Release()

You also shouldn't use Release() on a CConsole instance where the
instance is a static variable, global, function or class scope. Finally,
can't use it where the CConsole was a member variable. Ie.,

class Foo
{
CConsole o;
};

Anyway, use of delete this is common with reference counting
schemes where the count is maintained with the actual object.
Use of auto_ptr is not always a replacement for reference
counting.
Jul 22 '05 #8
Gianni Mariani <gi*******@mariani.ws> wrote:
Daniel T. wrote:
...

To put this another way, your code should make it explicit what object
is in charge of deleting what other objects. The code example muddies
the waters because it puts the 'delete' in CConsole, but CConsole isn't
really in charge of deleting itself, whatever object calling Release is
the one really in charge of the destruction...


I thought that the whole purpose of reference counting was that you
removed the "explicit" nature of what deletes what.

It's much more flexible to design a system where an object is deleted
when it is no longer "referenced". Of course, you have the issue of
circular referencing but *that* is a design problem.


True but the code example shown wasn't a reference count, it was a
simple delete wrapped in a "release" member-function.

Even so, reference counting isn't much use IMO when a "release" function
must be explicitly called. One ends up having to rely on all possible
client classes doing the right thing. No the best example would be
something like boost::shared_ptr. Note how all the code that tracks when
an object should be deleted is burried within a single class.
Jul 22 '05 #9
gr*****@dscpl.com.au (Graham Dumpleton) wrote in message news:<dc**************************@posting.google. com>...
"Daniel T." <po********@eathlink.net> wrote in message news:<po******************************@news03.east .earthlink.net>...
"solosnake" <solosnake@solosnake._remove_this_.fsnet.co.uk> wrote:
Whilst browsing Flipcode I noticed this code:
class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?


Can you guarantee that no CConsole object will ever be allocated on the
stack?


It also can't be an element in array, ie., can't have:

a = new CConsole()

and then use Release() on a member of the array. Ie.,
dangerous to say:

a[0].Release()


Whoops, I obviously mean't:

a = new CConsole[n]
Jul 22 '05 #10
No, it is not safe, but it is allowed.

It is used in certain situations where absolutely necessary, and the
implementor can guarantee certain conditions are met w/ the use of that
class/object, such as that the object cannot be allocated on the stack.

solosnake wrote:

Whilst browsing Flipcode I noticed this code:

class CConsole: public I_TextOutput
{
public:
...
void Release() { delete this; }
...
};

The 'delete this;' caught my eye. Is such code safe?

The full article is here :
http://www.flipcode.com/articles/art...nplugins.shtml

Thanks,

solosnake


--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Yongming Wang | last post by:
FuncA is called to delete itself with B's help. In vc 7.0, case 1 seems invalid, but case 2 is valid. Could anyone explain for me? class A { public: void FuncA() { B b(this);
3
by: maadhuu | last post by:
well,i am curious to know what would be the output of the following: delete this; basically , comment on this .
6
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to...
2
by: Bob Tinsman | last post by:
This problem shows up in Firefox 1.5.0.1 and Rhino 1.6R2. I've found that if I have an XML node expression that ends in a filter, I can't use it with the delete operator. In the following...
7
by: Alex Maghen | last post by:
I have a DataGrid control with a LinkButton command column that deletes the row. What I want to do is set it up so that there's a client-side Confirm alert BEFORE the actual Delete command gets...
3
by: Daniel Mark | last post by:
Hello all: I am looking the sample code posted on PIL website http://www.pythonware.com/library/pil/handbook/imagedraw.htm ################################################ <<Draw a Grey Cross...
1
by: MorgyTheMole | last post by:
I have a base class: class A { protected: static void* operator new (size_t size); void operator delete (void *p); public: void Delete(); }
5
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.