473,385 Members | 2,210 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,385 software developers and data experts.

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 2556

"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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.