Memory leak nightmare!!!

Discussion in 'C' started by Panagiotis, Oct 31, 2007.

  1. Panagiotis

    Panagiotis New Member

    Joined:
    Oct 31, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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!
     
  2. dharmaraj.guru

    dharmaraj.guru New Member

    Joined:
    Oct 23, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    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 |||.
     
  3. Panagiotis

    Panagiotis New Member

    Joined:
    Oct 31, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice