I'm learning C++ by reading the very good book C++ Primer and I'm learning how C++ deallocates memory by the delete keyword like C does with free. Java and Pascal do not have this mechanism for explcitly freeing memory. It could lead to errors in programs if they run for long time and variables are destroyed that are needed so it should not be trivialized.

In short I wonder if it's legal or advicable for instance in C++ for a variable to do this.delete() and delete itself. We mostly hear about freeing pointers in C and C++ and this is done with new free and delete keywords. Pascal also has pointers but Java has not. So in Java it should not be possible since you do not explicitly delete objects, C doesn't have objects so a struct couldn't free the memory it was allocated even if it was technically possible since C does not have objects and neither does Pascal.

So I suppose that leaves C++ for my question whether it is legal for an object to delete itself with something like this.delete()?

6 Answers
6

So, if you are very careful with was is done afterward, it's fine and legal for an object to "commit suicide" by doing delete this;

But, It's really not a good idea, especially because it means that your class should only be instanciated by new, as an allocation on te stack could cause the destructor to be called twice: by the delete this, and when going out of context.

Thank you for the answer. I notice that in C++ compared to other languages it is not unusual to risk "undefined bevaviour."
–
Niklas RosencrantzMay 28 '12 at 11:04

1

@NickRosencrantz: It is extremely unusual to willingly risk undefined behaviour in production code.
–
NPEMay 28 '12 at 11:06

3

@NickRosencrantz: in most other languages, undefined behavior doesn't exist. In C++, it should be avoided like the plague, because it is, by definition, unreliable. You can't trust your program once it's entered undefined behavior.
–
jalfMay 28 '12 at 11:09

@jalf I won't deliberately start it, I just never heard about undefined behavior before I studied C++ and therefore I thought I risked it. I've seen C, Pascal, assembler, Java and other languages and none of them had undefined behavior even for bugs. I suppose it's OK that undefined behavior exists in a language.
–
Niklas RosencrantzMay 28 '12 at 12:11

@NickRosencrantz: C certainly has undefined behavior. I'm not sure about Pascal. Java does not, and assembler does not
–
jalfMay 28 '12 at 12:53

my question whether it is legal for an object to delete itself with something like this.delete()?

Technically, it is legal for an object to perform delete this. However, there is a number of hugely important caveats that are explained in the FAQ.

It is also important to understand that delete this solves a very narrow technical problem. It does not really solve any big-picture questions about memory management and garbage collection. One direction worthy of further study is the use of smart pointers in C++.

It is entirely possible for an object to deallocate its own memory. However, it is very rarely used, for obvious reasons.

The most common usage is for implementing reference-counted memory management. When the caller invokes release() and the reference count hits zero, the object is deleted. Since this happens inside a member variable, it uses the this pointer to delete the instance (much in the same way you would call delete foo outside the object). For example:

(Note that the syntax you mention is not valid - delete is a keyword, not a method, and this is a pointer.)

There are several caveats to keep in mind though. Once this deletion has been called, the this pointer is no longer valid, nor are any of the data members. From that point onward, only non-instance references can be made (eg. to local variables, static methods and data, etc).