473,473 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

delete this

Is it ok to delete a class from within one of is own methods? It works on
VC7 but I want to know if this is generally or portably ok.
Thanks
Allan

Jul 22 '05 #1
18 2562

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
Is it ok to delete a class from within one of is own methods? It works on
VC7 but I want to know if this is generally or portably ok.
Thanks
Allan


Yes, but you must be careful not to access any member data or call any other
methods (since an implicit "this" pointer is passed and you just nuked
"this"). I'm sure there are other things to watch out for that others will
mention. Be careful; this is legal but tricky...
Jul 22 '05 #2

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
Is it ok to delete a class from within one of is own methods? It works on
VC7 but I want to know if this is generally or portably ok.
Thanks
Allan


Provided you don't use the object after you have done a 'delete this' it's
OK.

john
Jul 22 '05 #3

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
Is it ok to delete a class from within one of is own methods? It works on
VC7 but I want to know if this is generally or portably ok.
Thanks
Allan


It is OK provided that you don't try to do anything to members after the
delete.

It is also bad design (unless the method is static).
Jul 22 '05 #4
Nick Hounsome <nh***@blueyonder.co.uk> spoke thus:
It is also bad design (unless the method is static).


If the method is static, there is no this to delete...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #5

"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.com...

Yes, but you must be careful not to access any member data or call any other methods (since an implicit "this" pointer is passed and you just nuked
"this"). I'm sure there are other things to watch out for that others will mention. Be careful; this is legal but tricky...


Is this valid?
Will i be set and pB be destroyed or are these variables nuked right after
delete this ?

Class A
{
private:
int i;
B* pB;

public:
A() : i(0) { pB = new B(); }

~A()
{
i = 1;
delete pB;
}

void deleteMe()
{
delete this;
}
};
Jul 22 '05 #6

"xerix" <xe***@bla.nl> wrote in message
news:97***************************@news.multikabel .nl...

"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.com...

Yes, but you must be careful not to access any member data or call any

other
methods (since an implicit "this" pointer is passed and you just nuked
"this"). I'm sure there are other things to watch out for that others

will
mention. Be careful; this is legal but tricky...


Is this valid?
Will i be set and pB be destroyed or are these variables nuked right after
delete this ?

Class A
{
private:
int i;
B* pB;

public:
A() : i(0) { pB = new B(); }

~A()
{
i = 1;
delete pB;
}

void deleteMe()
{
delete this;
}
};


If your app. calls deleteMe(), the object must be on the heap, and you must
not otherwise try to deallocate the object.

void foo()
{
A *ptr = new A;
A var;

ptr->deleteMe();
delete ptr; // BANG!!! Double-dellocation; bad, bad program!

var.deleteMe(); // BANG!!! Deallocation of non-heap object; bad, bad
program!
}
Jul 22 '05 #7

"Nick Hounsome" <nh***@blueyonder.co.uk> wrote in message
news:dg******************@news-binary.blueyonder.co.uk...

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
Is it ok to delete a class from within one of is own methods? It works on VC7 but I want to know if this is generally or portably ok.
Thanks
Allan


It is OK provided that you don't try to do anything to members after the
delete.

It is also bad design (unless the method is static).


I use this in Win32 programming, there is one instance where I cant see
anywhere around it.
Thanks
Allan
Jul 22 '05 #8
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
Is it ok to delete a class from within one of is own methods? It works on
VC7 but I want to know if this is generally or portably ok.

No it is not. Always delete an object from the outside and place any cleanup
of resources you may use in the class at the destructor.


Ioannis Vranos

Jul 22 '05 #9
Nick Hounsome wrote:

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
Is it ok to delete a class from within one of is own methods? It works on
VC7 but I want to know if this is generally or portably ok.
Thanks
Allan


It is OK provided that you don't try to do anything to members after the
delete.

It is also bad design (unless the method is static).


Could you provide more details on why this is a bad design?

I'd consider this more likely to be a design that requires careful scrutiny and
understanding of the behavior and consequences.

Once place that I see this construct being appropriate is in a class that
implements (static-method) creation and reference counting (presuming that
auto_ptr and related template wrappers are not appropriate).
Jul 22 '05 #10
Ioannis Vranos wrote:

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:c5**********@news.freedom2surf.net...
Is it ok to delete a class from within one of is own methods? It works on
VC7 but I want to know if this is generally or portably ok.


No it is not. Always delete an object from the outside and place any cleanup
of resources you may use in the class at the destructor.


Sorry, my friend, you are mistaken.

delete this;

is perfectly acceptable within a class method, provided that the instance
(this, non-static data or virtual methods) is not subsequently referenced.
Jul 22 '05 #11
"Julie" <ju***@nospam.com> wrote
Nick Hounsome wrote:

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote
Is it ok to delete a class from within one of is own methods? It works on VC7 but I want to know if this is generally or portably ok.
Thanks
Allan
It is OK provided that you don't try to do anything to members after the
delete.

It is also bad design (unless the method is static).


Could you provide more details on why this is a bad design?

I'd consider this more likely to be a design that requires careful scrutiny

and understanding of the behavior and consequences.

Once place that I see this construct being appropriate is in a class that
implements (static-method) creation and reference counting (presuming that
auto_ptr and related template wrappers are not appropriate).


If I might chime in on this, it's bad design in that it reduces the cohesion of
the class by making it moonlight as its own lifetime manager. A cleaner design
is to have an external entity to manage the lifetime of the object(s). A rare
exception to this is when dealing with objects that "morph" (i.e., change type
during their lifetime), but even there, the use is questionable and can be more
cleanly implemented using the Cheshire Cat (a.k.a. letter-envelope) idiom.

Claudio Puviani
Jul 22 '05 #12
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...

Sorry, my friend, you are mistaken.

delete this;

is perfectly acceptable within a class method, provided that the instance
(this, non-static data or virtual methods) is not subsequently referenced.

Yes in usual implementations you are right. But in theory since the member
function is still executed after the end of the delete this; expression,
isn't it undefined behaviour? An implementor can choose implement the
function in a way dependent to *this for example. Except of the member
function implementation, C++ code may run in a mixed environment, an example
is .NET which i have been studying lately.

So strictly speaking i believe it is undefined behaviour. Unless the
standard mentions something else... And programmmatically speaking it is
also very bad coding approach.


Ioannis Vranos

Jul 22 '05 #13

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c5**********@chessie.cirr.com...
Nick Hounsome <nh***@blueyonder.co.uk> spoke thus:
It is also bad design (unless the method is static).


If the method is static, there is no this to delete...


I was reading the post rather than the title.
In the post he just said "Is it ok to delete a class from within one of is
own methods? " with no
mention of this.
Jul 22 '05 #14
Nick Hounsome wrote:
"Christopher Benson-Manica" wrote
Nick Hounsome <nh***@blueyonder.co.uk> spoke thus:
It is also bad design (unless the method is static).


If the method is static, there is no this to delete...


I was reading the post rather than the title.
In the post he just said "Is it ok to delete a class from within one of is
own methods? " with no mention of this.


Oh. Well, that makes it perfectly acceptable. (??)

--
Regards,
Buster.
Jul 22 '05 #15
>
If I might chime in on this, it's bad design in that it reduces the cohesion of the class by making it moonlight as its own lifetime manager. A cleaner design is to have an external entity to manage the lifetime of the object(s). A rare exception to this is when dealing with objects that "morph" (i.e., change type during their lifetime), but even there, the use is questionable and can be more cleanly implemented using the Cheshire Cat (a.k.a. letter-envelope) idiom.

Claudio Puviani


The reason I use it is that I have a class called ChatWindow which I have
made. This creates a window and controls the message loop etc. of my win32
program. When a user clicks to close the window, I wish for the program to
still run - I have a roster window which I consider the main hub of the
program. So, I recieve a WM_CLOSE message within one of the methods of the
class which should then force the cleanup of the class etc. I currently use
"delete this" at the very end of this method and things seem to work. There
is a way I can get around this but it is quite messy in my opinion so I am
keen to keep it this way.
Allan
Jul 22 '05 #16
Dave wrote:
If your app. calls deleteMe(), the object must be on the heap, and you must
not otherwise try to deallocate the object.

void foo()
{
A *ptr = new A;
A var;

ptr->deleteMe();
delete ptr; // BANG!!! Double-dellocation; bad, bad program!

var.deleteMe(); // BANG!!! Deallocation of non-heap object; bad, bad
program!
}

To prevent these BANGs, make the destructor private or protected. Then
these statements would be illegal and you'd get compile time error:

A var; // destructor not accessible
delete ptr; // same here

--
Ahti Legonkov

Jul 22 '05 #17
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote

If I might chime in on this, it's bad design in that it reduces the
cohesion of the class by making it moonlight as its own lifetime
manager. A cleaner design is to have an external entity to
manage the lifetime of the object(s). A rare exception to this
is when dealing with objects that "morph" (i.e., change type
during their lifetime), but even there, the use is questionable
and can be more cleanly implemented using the Cheshire
Cat (a.k.a. letter-envelope) idiom.


The reason I use it is that I have a class called ChatWindow
which I have made. This creates a window and controls the
message loop etc. of my win32 program. When a user clicks
to close the window, I wish for the program to still run - I have
a roster window which I consider the main hub of the program.
So, I recieve a WM_CLOSE message within one of the methods
of the class which should then force the cleanup of the class etc.
I currently use "delete this" at the very end of this method and
things seem to work. There is a way I can get around this but
it is quite messy in my opinion so I am keen to keep it this way.


In the long run -- and certainly on large projects -- you'll find that it's a
lot less messy to avoid giving a single class too many purposes. You might want
to look up the "Chain of Responsibility" patters that offers a clean solution
to what you're trying to do.

Claudio Puviani
Jul 22 '05 #18
One joyful day (Wed, 14 Apr 2004 08:09:55 +0100 to be precise), "Nick
Hounsome" <nh***@blueyonder.co.uk> decided that the Usenet community
would benefit from this remarkable comment:
I was reading the post rather than the title.
In the post he just said "Is it ok to delete a class from within one of is
own methods? " with no
mention of this.


Just as an aside, is a static function a method? I thought "method" was
the general OO name given to a function which operates on an instance of
an object and which must be called from that object. That is, a function
may be called with no context but a method must always be invoked from
an instance.

I that is the case then in C++ there will always be a this pointer
available to a method. :o\

Mark Wright
- ma**@giallo.demon.nl

================Today's Thought====================
"In places where books are burned, one day,
people will be burned" - Heinrich Heine, Germany -
100 years later, Hitler proved him right
================================================== =
Jul 22 '05 #19

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

Similar topics

2
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the...
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
13
by: Amy | last post by:
Hello, We are developing C++ appplications for PDAs where memory is limited, so we want to do memory management by ourselves --- pre-allocated a big chunk and overwrite new and delete to call...
5
by: mandatory | last post by:
hi, i have a pointer to a class, which is dynamically allocated with new. myClass *mC = new myClass(); What if i at a later point in my code i have this: void *pointer;
8
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
10
by: junw2000 | last post by:
Hi, Below is a small code about memory allocation and deallocation. #include <cstdlib> #include <iostream> using namespace std; class X { public: void* operator new(size_t sz) throw (const...
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
0
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...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.