Tuesday, May 21, 2024

C++: Destructor

The following tech recipe describes how to implement a C++’s Destructor efficiently.

When writing a destructor, it is important to understand why and what you are writing before you begin.
*A structure and a pointer tech recipe are on the way to explain anything thing complex here.
To point or rather to not point: a destructor removes pointers in the case of manually watching your memory leak. This method is automatically called whenever an object loses scope.

The Scope is part of the code (e.g., method, class, etc.) in which a variable or parameter can be accessed.

Thus, it takes care of all local and dynamic variables for us, preventing memory leak if it is written properly.

*The example will be written as if it were inside a class which has a structure within it implementing a linked list by the pointer:
Next* next;
(which, of course, the struct is called Next)

Destructor Syntax:

~Destructor() {}

-A destructor never takes any parameters. Its only parameter is the implied one accessible by this.
-It always starts with a ~, tild or tilda, as it is known.

Now, within a structure implementing a linked list, this becomes the most crucial part of your code. The structure not only has things pointing to it, but it is also pointing to other objects of its own type. These all need to be handled dynamically, but how is this done?
*As stated previously, C++ will call this for every object, but we have no way to be sure that it will be efficient and call them in the reverse order in which they are pointing. If we were to delete the lead pointer only, it is possible that C++ would accept this as a completed destruction, which is not true.
As long as the other pointers and objects are linked, the memory is not freed, and we have memory leak.

So, we need to delete all pointers below the current this object to insure a proper destruction. If all goes well, each object will only destroy one pointer, but we cannot be sure of this.

This will require a loop and a temp variable (which we will also destroy at the end of the destructor).

Complete Linked List Destructor:
*This is within the Next structure, so it gets the title of its class or struct.

Next* n = this->next;
Next* nn = NULL;
nn = n->next;
delete n;
n = nn;
nn = NULL;
n = NULL;

-This loop is based on n which is true as long as n is not NULL. (e.g., There is another Next object in the linked list.)
This code iterates through the linked list objects and deletes the pointers one by one, once it is finished with all pointers below itself setting the temporary variables to NULL. This will allow the GC or Garbage Collector to destroy them.

This example is meant to improve your knowledge of memory leak. The less leakage there is in your own programs, the better because we all know that Operating Systems are not leak free. The more help we can give them, the better.

Questions/Comments: [email protected]
-William. §



Please enter your comment!
Please enter your name here

Most Popular


Recent Comments

error: Content is protected !!