473,569 Members | 3,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 2133
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@solo snake._remove_t his_.fsnet.co.u k> 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@solo snake._remove_t his_.fsnet.co.u k> wrote in message
news:bt******** **@news5.svr.po l.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***@blueyond er.co.uk> wrote:
"solosnake" <solosnake@solo snake._remove_t his_.fsnet.co.u k> 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********@eat hlink.net> wrote in message news:<po******* *************** ********@news03 .east.earthlink .net>...
"solosnake" <solosnake@solo snake._remove_t his_.fsnet.co.u k> 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*******@mari ani.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_p tr. 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.c om.au (Graham Dumpleton) wrote in message news:<dc******* *************** ****@posting.go ogle.com>...
"Daniel T." <po********@eat hlink.net> wrote in message news:<po******* *************** ********@news03 .east.earthlink .net>...
"solosnake" <solosnake@solo snake._remove_t his_.fsnet.co.u k> 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

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

Similar topics

5
1637
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
1719
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
1987
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 explicitly call delete on these instances when i no longer need them?
2
2030
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 example, the delete operation has no effect: var z = <abc><foo a='1'>1</foo><foo a='2'>2</foo></abc>; alert(z.foo.(@a == '2')); delete z.foo.(@a ==...
7
5260
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 called on the server-side. That's easy to do with normal buttons, etc. as follows... <asp:Button ID="ConfirmBtn" Text="ConfimMe!"...
3
6451
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 Over an Image>> import Image, ImageDraw
1
1667
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
588
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 following class as an example: class Test { public: static void print_message(char *s) { printf("%s\n", s); } void delete_me()
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.