Delete This looks very strange, I remember in "C + + Primer" mentioned delete this, but I have forgotten where, also has not been found (because there is no electronic version, so has not found ~). What is mentioned in C + + Primer is that using delete this in a destructor causes an infinite invocation of the destructor, which eventually results in a stack overflow. I also read some online, many people feel that you should not use the delete this, because it will cause some problems. But the delete this is also useful, just like a goto statement, should not be beat him (Goto statement is actually a good thing:-D). In the use of COM components, there are many places where delete this is used. In the implementation of the template for smart pointers in C + +, delete this is also used. So today I'm going to talk about my own understanding of delete this.
to delete this, you first have to figure out how the delete works. Here's an MSDN story:
for an object that is not a class type (class, struct, or union), the global delete operator is called. For objects of class type, if the delete expression is a unary scope resolution operator (::) Begins, the name of the deallocation function is resolved in the global scope. Otherwise, the delete operator calls the destructor for the object before releasing the memory (if the pointer is not NULL). The delete operator can be defined for each class, and the global delete operator is called if the definition is not present for the given class. If the delete expression is used to dispose of a class object whose static object has a virtual destructor, the release function is resolved through the virtual destructor of the object's dynamic type. (links are as follows: http://msdn.microsoft.com/zh-cn/library/z5308zac.aspx)
The second sentence I did not understand, but the others still understand. In general, the delete operator calls the destructor before releasing the memory that the pointer points to. So the pointer behind the delete must point to the dynamically allocated memory, which means it must be the new memory. Since it is freeing up memory, of course we have to look at what is stored inside a function object so that we know what is removed.
One, virtual function table
A C + + object stores not only the member variables of the object, but also pointers to virtual functions. Then the virtual function of the pointer together is a virtual function table. Each item in the virtual function table points to the address of the virtual function. In this way, C + + implements the dynamic binding of pointers or references (or implementations of polymorphic bars ~). Without this table, a pointer to a base class is declared in C + +, and the pointer is actually to a subclass, and if a virtual function of the object is called, the member function of the child class is called instead of the member function of the parent class. In VC + +, an object is to save virtual function tables and member variables in this way. (Note: The following lab environment is Windows8.1, and the IDE is Visual Studio 2013.) )
This is the content saved by two different objects of the same class. We can see that the object holds a variable called _vfptr first. Let me yy, it should be virtual function ptr abbreviation, as if this means oh. He is actually a virtual function table, followed by a member variable.
In this picture, there are two pointer objects in two classes. The P3 class is CC, which inherits from the P2 class delthis. P3 declares the pointer to the base class Delthis, but I see that the contents of the virtual function table of P3 and P2 are not the same, and the specific function is not the same as the member function.
So, when we delete an object, we delete only its virtual function table and member variables, and nothing else moves.
Second, delete this
Next, let's say this wonderful delete this. The first thing to make clear is that the delete this is syntactically absolutely error-free. But the use of the time must be noticed. First, as mentioned earlier, you cannot use delete this in a destructor, which can cause a stack overflow. There is a certificate:
stack overflow is a stack overflow. Then look at:
program because of the stack overflow interrupt, you can see that the program is constantly calling the Delthis class's destructor before the interrupt. And then there is no time to terminate, so the call is more after the stack overflow.
But we don't have a problem using delete this in the non-destructor member function. Before the delete this is called, the contents of the this pointer are as follows:
After using the delete this, the contents of this point become:
So using delete this in a general member function is not problematic (including using delete this in the const function).
but be careful when you use delete this, the object must be stored in the heap, or the following error will occur:
delete this application in COM is primarily to remove COM objects. COM objects are typically created by a class factory provided by a COM component, but do not provide a release function, and letting the user handle the release of a COM object is inherently cumbersome and unlikely to be implemented (it should be as if the release of the smart pointer is to be released manually). Only by judging the function that reduces the reference count, if the reference count is 0, freeing the COM object's resources, but the reference count function is provided by the COM object, you can only select Delete this.
Okay, delete this is written here, welcome to the brick.
"COM Principles and Applications" digression--c++ virtual function table and delete this