Sunday, January 3, 2010

List push_back() function causes a crash

Most people while working with list iterators
are sure to have come across this problem
when working in C++ at some point in their lives.

Symptoms:
Program crashes abruptly.

Debugging using the gdb debugger shows that
the crash occurs while executing the push_back()
function. I was using the Devshed C++ development
environment and noticing similar crashes with my
code. However, it turns out the problem did not have
anything to do with push_back() function. Rather,
it was happening because, appropriate memory management
was not being done for other allocated memory structs.
Typically, re-assignment of pointers which were not
appropriately free()'d before re-initialization was
resulting in these crashes. So as such push_back() is
not crashing but other poor memory management was
responsible. Please also check that you are not passing
a NULL as an argument for push_back(), which could
possibly cause a crash.

free() causes C++ program to crash

The free() function is used as a means for
explicitly telling the C++ compiler to delete
the memory allocated to the pointer passed
as an argument in the free() function.


A typical type of crash can occur if
free() is called on a pointer to which
memory is not allocated or when the pointer
is not initialized. In this case, the pointer
points to an invalid memory location resulting
in a fatal error when trying to free memory
allocated at that location.

To remedy this problem, please verify if the
pointer is being correctly initialized by putting
appropriate checks.