delete this;

M

Martin Vorbrodt

some butthole asked me an interview question:

can you delete "this" pointer in a member function of a class, like this:

class C {
public:
void foo() { delete this; }
};

my answer was... undefined behavior, maybe? object kills itself and frees
it's own memory, not a good thing in my opinion.

the guy tried to convince me that it's a common practice to "reload the
object", and that this delete would not actually free memory, only call
destructor(s).

now, i know C++ pretty darn well, but I'm not the all mighty guru... yet.

so, am I stupid, or was he full of shit?

Thanx
 
W

W Marsh

some butthole asked me an interview question:

can you delete "this" pointer in a member function of a class, like this:

class C {
public:
void foo() { delete this; }
};

my answer was... undefined behavior, maybe? object kills itself and frees
it's own memory, not a good thing in my opinion.

the guy tried to convince me that it's a common practice to "reload the
object", and that this delete would not actually free memory, only call
destructor(s).

now, i know C++ pretty darn well, but I'm not the all mighty guru... yet.

so, am I stupid, or was he full of shit?

Thanx

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15
 
S

Scott McPhillips [MVP]

Martin said:
some butthole asked me an interview question:

can you delete "this" pointer in a member function of a class, like this:

class C {
public:
void foo() { delete this; }
};

my answer was... undefined behavior, maybe? object kills itself and frees
it's own memory, not a good thing in my opinion.

the guy tried to convince me that it's a common practice to "reload the
object", and that this delete would not actually free memory, only call
destructor(s).

now, i know C++ pretty darn well, but I'm not the all mighty guru... yet.

so, am I stupid, or was he full of shit?

Thanx

You're both wrong. It is a perfectly legitimate thing to do. It works,
it does execute the destructor, and does free the memory.
 
S

Sumit Rajan

Martin said:
some butthole asked me an interview question:

can you delete "this" pointer in a member function of a class, like this:

class C {
public:
void foo() { delete this; }
};

my answer was... undefined behavior, maybe? object kills itself and frees
it's own memory, not a good thing in my opinion.

the guy tried to convince me that it's a common practice to "reload the
object", and that this delete would not actually free memory, only call
destructor(s).

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15

Regards,
Sumit.
 
R

Rolf Magnus

Martin said:
some butthole asked me an interview question:

can you delete "this" pointer in a member function of a class, like this:

class C {
public:
void foo() { delete this; }
};

my answer was... undefined behavior, maybe?

It depends on how the object was created. The behaivor is well-defined if
the object was created with operator new. If it's an automatic or static
objec, the behavior is undefined.
object kills itself and frees it's own memory, not a good thing in my
opinion.

Maybe, but that wasn't the question, as I see it.
the guy tried to convince me that it's a common practice to "reload the
object", and that this delete would not actually free memory, only call
destructor(s).

Well, that's wrong. Unless you give it a null pointer (in which case it does
nothing) or a pointer that didn't come from new (in which case, the
behavior is undefined), delete always frees memory. I have no idea
what "reload the object" means. Sometimes, an explicit destructor call and
placement new are used in operator=.
 
A

al pacino

hi,
hey same kind of question was asked to me too!
he asked me if we delete ' this '
in destrcutor what will happen?
i.e
class foo{
public:
//
~foo()
{
delete this;
}
};
i think this wud result in recursive calls to destructor and stack wud
overflow
 
N

Noah Roberts

Martin said:
the guy tried to convince me that it's a common practice to "reload the
object", and that this delete would not actually free memory, only call
destructor(s).

Yeah, many times the person interviewing doesn't know shit.
 
K

Kai

Hi,

Actually, 'delete this;' is legal. The compiler doesn't mind, and would
really delete 'this'. But it's stupid under most situation. see the
following from
http://www.progsoc.uts.edu.au/lists/progsoc/2000/October/msg00010.html

Title: RE: [ProgSoc] delete self in c++
Yeah it's okay to do this .. it's most often used inside
the constructor to destroy the object if the object itself
isnt valid.

Generally speaking, I'd recommend against using "delete this" in code.
The main problem is that the object can only be created via "new". If
the object is declared "auto" (i.e. not a pointer or reference) and
"delete this" is called it will fail because "this" was not allocated
with new and the program will crash. If you really want to use "delete
this" in your own code, make the destructor protected, preventing other
classes from deleting the object and causing a compiler error if an
auto variable of this class is ever declared.

If you want to use in it a constructor, I'd recommend against it for
the above reason and also because the calling function has no way of
knowing whether the object encountered an error - calling one of the
object's functions will result in a crash. The object fails to
instantiate itself will have to modify some form of global variable
such as errno, SetLastError(), IErrorInfo, etc, which, although
acceptable, is hardly ideal.

A much better way to report an error in an object's constructor is to
use a static "factory" function to encapsulate the construct. E.g.
static Dog* Dog::CreateDog(int legs) which can return a NULL pointer on
an error.

Alternatively, the constructor could throw an exception. Don't worry
about memory leaks, ANSI-compliant compilers should automatically
deallocate the object with calling the constructor if a newed object
throws an exception in its constructor. Check your compiler doco for
this.

The only situation I've seen "delete this" where I consider it an
acceptable use is the the thread or window classes from MFC. The
intention with these classes is to instantiate them, remove all
references to them and let the objects manages their own lifespans.
Also a good idea to check for null after any member function
that could cause the object to be destroyed.

I haven't got ARM (or a similar tome) handly but I suspect that
modifying "this" in a member funcion is illegal. Either way, changing
"this" in a member function will not affect the value of the pointer
the function was called through. E.g.

void zero_ptr(char* s)
{
// This line only changes the local version of s.
s = NULL;
}

....

char *s = "Hello, world!";
zero_ptr(s);
printf(s); // Shows "Hello, world!"

Anthony Langsworth
Software Developer
Computing Edge
mailto:[email protected]
 
N

Noah Roberts

Kai said:
Hi,

Actually, 'delete this;' is legal. The compiler doesn't mind, and would
really delete 'this'. But it's stupid under most situation. see the
following from
http://www.progsoc.uts.edu.au/lists/progsoc/2000/October/msg00010.html

Title: RE: [ProgSoc] delete self in c++
Yeah it's okay to do this .. it's most often used inside
the constructor to destroy the object if the object itself
isnt valid.

Generally speaking, I'd recommend against using "delete this" in code.
The main problem is that the object can only be created via "new". If
the object is declared "auto" (i.e. not a pointer or reference) and
"delete this" is called it will fail because "this" was not allocated
with new and the program will crash. If you really want to use "delete
this" in your own code, make the destructor protected, preventing other
classes from deleting the object and causing a compiler error if an
auto variable of this class is ever declared.

You would also want to privatize constructors and implement static
factory methods to be sure the object is always created with new.
 
A

Alf P. Steinbach

* Noah Roberts:
* Kai:

You would also want to privatize constructors and implement static
factory methods to be sure the object is always created with new.

No, that's an /alternative/ to making the destructor protected (or private).

It is IMO generally an inferior alternative.

See section 1.3.3 of my little introduction to pointers at <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>, which
also, in section 1.3.5, discusses how to in-practice disable direct use
of new, e.g. for an object that must assume it's managed via a smart
pointer of some particular kind.
 
T

tedu

Rolf said:
what "reload the object" means. Sometimes, an explicit destructor call and
placement new are used in operator=.

does this have any advantage over creating init() and destroy() member
functions?
 
R

Rolf Magnus

tedu said:
does this have any advantage over creating init() and destroy() member
functions?

Not sure if that could be seen as an advantage, but you can initialize
reference and const members in the constructor, but not in an init()
function. Hmm, actually, I have used that so that I can have a reference
member and still also an operator= that can change that reference.
 
T

Tom

It is legal, but I don't like it. I guess it is used when this object
need control its life by itself. In most cases, its life is controlled
by others. Or, the object is allocated in heap, but I want it to be
released automatically.

For the objects in DLL, I like to define a corresponding smart pointer
in DLL to release it (not template, it doesn't work).
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Martin Vorbrodt wrote:
-snip-

In the topic line you wrote that I should delete your message. I can't......

My newsreader complaints about: "This message does not appear to be from
you. You may only cancel your own posts, not those made by others."


LOL :)


Best regards / Med venlig hilsen
Martin Jørgensen
 
S

sillyemperor

Tom said:
It is legal, but I don't like it. I guess it is used when this object
need control its life by itself. In most cases, its life is controlled
by others. Or, the object is allocated in heap, but I want it to be
released automatically.

For the objects in DLL, I like to define a corresponding smart pointer
in DLL to release it (not template, it doesn't work).

You are right.But it`s used widely in LIB.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top