Beruflich Dokumente
Kultur Dokumente
Memory leak occurs when programmers create a memory in heap and forget to delete it.
Memory leaks are particularly serious issues for programs like daemons and servers which by
definition never terminate.
Example:
Note: You can NOT use the C++ delete call. The strdup() function is part of
the C library and you must use free().
2. Programmer must free() malloc()'ed memory:
Also for calloc(), malloc() and realloc();
Check for memory allocation errors. Can't free it if it didn't get allocated.
If there was insufficient memory to permit the allocation, malloc() will return
NULL and the error code will be set to ENOMEM. Note that a call to malloc()
with a size of zero may also return NULL
3. Programmer must delete new'ed memory:
New/delete is preferred to malloc()/free() because it can initialize the memory
and it invokes the constructor for new objects. New/delete also point to the
correct memory type. Note on mixing source code containing new/delete and
malloc()/free(). This is not a problem with the GNU C++ compiler but this is
not guaranteed for all C++ compilers.
If you are counting on the destructor to delete memory allocated in the constructor
beware of this mistake as it will cause a memory leak. Use a virtual destructor to
avoid this problem. The ~BaseClass() destructor is called and then the destructor
~DerivedClass() is chosen and called at run time because it is a virtual destructor. If
it is not declared virtual then only the ~BaseClass() destructor is called leaving any
allocated memory from the DerivedClass object to persist and leak. The behavior of
this error is undefined so don't do it.
The same ill effect can be achieved with a C style cast to a class of less scope which
will dumb down the destructor to that which may not execute all the freeing of the
original class. A C++ style dynamic cast may prevent this error as it will recognize
the loss of translation and not allow the cast to take place resulting in a traceable
crash rather a tough to find memory leak
free(ptr);
return;
Memory Corruption:
-
Buffer overflow:
Example 1:
Overwrite beyond allocated length - overflow.
Example 2:
Index of array out of bounds: (array index overflow - index too large/underflow negative index)
Incorrect use of delete: The delete must match the use of new.
Exception Errors:
Freeing memory never allocated: If you use a constructor to allocate memory but
an exception is thrown before all is allocated, the destructor needs to be aware
that fact or else it may try to free memory which was never allocated.
Also the converse is true. If the destructor throws an exception, subsequent
steps which free memory may not be executed. This applies to the destructor
and all nested destructors which handle/re-throw the exception while the stack
unwinds.
BTW: Resources tied to the lifetime of the object is known as RAII (Resource
Acquisition Is Initialization), where the resource allocation is done during object
creation in the constructor and resource deallocation is done by the destructor.
-
Pointer persistence:
Function returning a pointer from the stack which can get overwritten by the
calling function (in this case main()):
This is also true for a local variable within the scope of only { and } as well as for
the scope of a function.
-
If the pointer is passed around as an argument and does not get passed
correctly, one may try to free the incorrect pointer.
-
If mixing the object base class and derived class when passing an object by
value as a function parameter, make sure that you understand what may be lost.
Note that this is a cautionary warning to be careful and not necessarily an error as it
depends on the intent and use. This can be used properly.
-
Copying an object:
Don't use memcpy() or any bit for bit copy function to copy an object. It will not
execute the class constructor. What kind of person would do this?? Passing an
object in a va_arg() list will result in a bit for bit copy and will not use the default
copy constructor.
Link:
[1] http://www.yolinux.com/TUTORIALS/C+
+MemoryCorruptionAndMemoryLeaks.html
[2] https://msdn.microsoft.com/en-us/library/x98tx3cf(v=vs.140).aspx