By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

delete this;

P: n/a
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
Apr 18 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Tue, 18 Apr 2006 10:12:03 -0400, "Martin Vorbrodt"
<mv*******@poczta.onet.pl> wrote:
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-lit...html#faq-16.15
Apr 18 '06 #2

P: n/a
Martin Vorbrodt wrote:
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.

--
Scott McPhillips [VC++ MVP]

Apr 18 '06 #3

P: n/a

Martin Vorbrodt wrote:
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-lit...html#faq-16.15

Regards,
Sumit.

Apr 18 '06 #4

P: n/a
Martin Vorbrodt wrote:
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=.

Apr 18 '06 #5

P: n/a
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

Apr 18 '06 #6

P: n/a

Martin Vorbrodt wrote:
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.

Apr 18 '06 #7

P: n/a
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/.../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:an******@nospam.computingedge.com.au

Apr 18 '06 #8

P: n/a

Kai wrote:
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/.../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.

Apr 18 '06 #9

P: n/a
* Noah Roberts:
* Kai:
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.


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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 18 '06 #10

P: n/a
Rolf Magnus wrote:
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?

Apr 18 '06 #11

P: n/a
tedu wrote:
Rolf Magnus wrote:
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?


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.

Apr 19 '06 #12

P: n/a
delete this; is very useful when you have to release a object created
in a DLL.

Apr 19 '06 #13

P: n/a
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).

Apr 20 '06 #14

P: n/a
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

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 20 '06 #15

P: n/a

Tom wrote:
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.

Apr 20 '06 #16

P: n/a
You are right.But it`s used widely.

Apr 20 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.