Tuesday, June 19, 2007

How to resolve memory leakages in C++ language?

Solution is Smart Pointer

A Smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the use of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.

The use of pointers is a major source of bugs: the constant allocation, de-allocation and referencing that must be performed by a program written using pointers makes it very likely that some memory leaks will occur. Smart pointers try to prevent memory leaks by making the resource de-allocation automatic: when the pointer to an object (or the last in a series of pointers) is destroyed, for example because it goes out of scope, the pointed object is destroyed too.

Several types of smart pointers exist. Some work with reference counting, others assigning ownership of the object to a single pointer. If the language supports automatic garbage collection (for instance, Java), then this use of a smart pointer is unnecessary.

In C++ language, smart pointers may be implemented as a template class that mimics, by means of operator overloading, the behavior of traditional (raw) pointers, (e.g.: dereferencing, assignment) while providing additional memory management algorithms.

Smart pointers can facilitate intentional programming by expressing the use of a pointer in the type itself. For example, if a C++ function returns a pointer, there is no way to know whether the caller should delete the memory pointed to when the caller is finished with the information.

some_type* ambiguous_function(); // What should be done with the result?

Traditionally, this has been solved with comments, but this can be error-prone. By returning a C++ auto_ptr,

auto_ptr obvious_function1();

the function makes explicit that the caller will take ownership of the result, and further more, that if the caller does nothing, no memory will be leaked. Similarly, if the intention is to return a pointer to an object managed elsewhere, the function could return by reference:

some_type& obvious_function2();

Example

Let SmartPointer be a template smart pointer for class X.

void test_smartpointers()
 {
 //first, we create two objects and we keep raw pointers to them
 //since these pointers are not smart, they will not affect the object lifecycle
 Object* obj_1 = new Object();               
 Object* obj_2 = new Object();       
 
 //then we declare two smart pointers and we assign them with the objects
 //both obj_1 and obj_2 will have counter==1
 SmartPointer p = obj_1;                  
 SmartPointer q = obj_2;
 
 //now we assign p into q, yielding obj_1.counter==2
 //obj_2 will be destroyed because its counter reaches 0
 q = p;
 
 //we assign q with NULL
 //obj_1.counter reaches 1
 q = NULL;
 
 //now we create a new object, and we assign its address to the smart pointer
 //it will be automatically destroyed before leaving the scope
 //obj_1 will be destroyed because its counter reaches 0
 p = new Object();
 
 //finally, we create another object which will be only referenced by a raw pointer.
 //obj_3 will be lost and there will be a memory leak
 Object* obj_3 = new Object();               
}

No comments: