Memory leak nightmare!!!

Panagiotis's Avatar, Join Date: Oct 2007
Newbie Member
Hello all,
During the testing of a large application I am maintaining I came across a segfault which only occured on newer versions of gcc and only with an -O2 optimization level. I tried to debug it.

However -O2 optimizaion doesnt allow access to some variables because they are optimized out. So i went around the old fashion way(prints). Somewhere along the execution a pointer got meddled with and changed value. Unfortunatelly that happens inside a very big function call in which i cant monitor the value of the pointer to see where it changes.

Here is the really anoying part: if i so much as touch the &pointer to monitor it, the value never changes an the program runs correctly.

Any ideas on how to go about debugging this?

Thank you!
0's Avatar
Go4Expert Member
Can you please give us the problemetric code snippet and point out where you think the culprit can be?
Your desciptions are generic and from it we can not make out anything.

||| Dharma |||.
Panagiotis's Avatar, Join Date: Oct 2007
Newbie Member
Having found the solution I'll share my strategy,
- If debugger is giving you a hard time use prints in the code to find out what is causing the segmentation fault.
- The most common cause for segmentation is dereferencing invalid pointers.
- If the pointer that is invalid is a local variable then most probably there is something wrong in a called function's local variables (insuficient local buffer size)
- If the pointer going wrong is in the heap then you sould also look into leaks in the heap
- Again if debugger isn't allowing proper inspection due to optimization, gradually comment out code in the area of the segfault until the segfault stops.

- In the case where the error isn't systematically reproducable (the worst case ) try using a simple memory manager that wraps memory allocation to keep tags on what is allocated an who is referencing what.