Call the Delete this in the member function of the class

Source: Internet
Author: User

Can I call delete in a member function of a class This? The answer is yes, it can be called, and many older libraries have this code. Suppose this member function name is release, and delete this is called in this release method, then this object can do other things after calling the release method, such as other methods of invoking the object? The answer is still yes, the call to release can also invoke other methods, but there is a premise that the called method does not involve the object's data members and virtual functions. Speaking of which, I believe everyone can understand why this is so. The root cause lies in the function of the delete operator and the memory model of the class object. When a class object is declared, the system allocates memory space for it. In the memory space of a class object, only data members and virtual function table pointers do not contain code content, and the member functions of the class are placed separately in the code snippet. When you call a member function, you implicitly pass a this pointer, letting the member function know which object is currently calling it. When delete this is called, the memory space of the class object is freed. Any other function calls made after delete this will work as long as the contents of the this pointer are not involved. When this pointer is involved, such as manipulating data members, calling virtual functions, and so on, unexpected problems occur. Why is it an unexpected problem? DeleteThis does not release the memory space of the class object, then this memory should have been returned to the system, no longer belong to this process. According to this logic, there should be a pointer error, no access to the system crashes such as the problem is right ah? This problem involves the memory management strategy of the operating system.Deletethis frees the memory space of the class object, but the memory space is not immediately recycled into the system, it could be a buffer or some other reason, causing the memory space to be temporarily not retracted by the system. At this point the memory is accessible, you can add 100, plus 200, but the value is indeterminate. When you get a data member, you get a string of very long uninitialized random numbers, and the possibility of accessing a virtual function table with invalid pointers is very high, causing the system to crash. After a general understanding of what happens when you call delete this in a member function, take a look at another question, if you call delete in the destructor of the class This, what will happen? The experiment tells us that it will cause a stack overflow. The reason for this is simple: the essence of Delete is "to call one or more destructors for the memory that will be freed, and then to free up memory" (from effective C + +). ObviouslyDeleteThis will call the destructor of this object, and the destructor calls the delete This, resulting in infinite recursion, causing stack overflow and system crashes. --------------------I'm a sub-interface--------------------The above is a Daniel's analysis, and in the actual operation of the use of Delele this does directly occur error. This is because: call Delete in the member function This, the destructor for the class is called first, and the this pointer is deleted, and a pointer error occurs. The following is an error that occurs when you use delete this in Xcode:malloc: * * * ERROR for Object 0xbffffa18: Pointer being freed is not allocated//Note that 0xbffffa18 is the address of this***SetA breakpointinchmalloc_error_break to debug and using delete this in VS2010 is a direct result of debug assertion failed! The specific description is: InvalidNULLPointer Summary: Call delete in the member function This, a pointer error is caused, and a delete is called in the destructor This, which causes a dead loop, causing a stack overflow. Ps:this is an additional implicit parameter of a member function in a class that is a pointer to a class object that is bound to the object that invokes the member function. At the same time 1. In a normal non-const member function: The type of this is a const pointer to a class type that can change the value that this points to, but cannot change the address saved by this .2. In the const member function, the type of this is a const pointer to the Const class type object that cannot change the object that this is pointing to or change the address saved by this. Note:Delete  Thisusage and precautions This object must be assigned with the new operator (not with new[], nor with placementNew, nor is it a local object, nor a global object);DeleteThis does not access any of the object's member variables and virtual functions (DeleteThis reclaims the data, which includes the object's data members as well as the vtable, excluding the function code);DeleteThis pointer is no longer accessible after this. In other words, you can't check it, compare it to other pointers, compare it to null, print it, convert it, and anything else; The basic means of guaranteeing the above taboo list can include: privatization of the destructor (if there are subclasses, protected to ensure that subclasses inherit correctly) --to ensure that the object must use new to allocate memory on the heap, provided (can be in the base class only) Destroy (void) function, there is only one sentence of delete This--to ensure that third parties are able to reclaim the allocated memory;

Call the Delete this in the member function of the class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.