Thread: corrupted stack pointer

  1. #1
    Registered User
    Join Date
    Oct 2009

    corrupted stack pointer

    I have a thread A in my code. Even though the stack is not full, I sometimes see that I have an invalid value of the stack pointer (the stack pointer value is outside the stack size range for that thread) and see a stack overflow error. Any tips on what are the possible ways this could happen? and how I could possibly debug this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The typical way for such a thing to happen is to go walking off the end of an array.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Quote Originally Posted by tabstop View Post
    The typical way for such a thing to happen is to go walking off the end of an array.
    But that doesn't usually cause the stack pointer to point somewhere weird.

    Normally, the only way the stack pointer could become invalid is if you entered an infinite recursion, or tried to allocate too big of an object on the stack.

    Given that it crashes, I assume he's not just seeing the stack pointer from some other thread (it's easy to confuse yourself with the debugger) and in fact there is something wrong. I'd start by examining any recursive functions or functions which allocate large stack variables.

    EDIT: If this is on Windows, there is a strange thing involving floating point that can result in a "stack overflow" (it's not, it's Windows messing up) but we'll get there when we get there...
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    One way in VS is to add a breakpoint to any loops of suspected infinitely recursive functions, that breaks at or after a certain number of hits.
    But then, you haven't told us what environment you're using yet
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. corrupted stack???
    By Abda92 in forum C Programming
    Replies: 12
    Last Post: 01-31-2008, 03:05 AM
  3. stack around the variable corrupted
    By chintugavali in forum C++ Programming
    Replies: 2
    Last Post: 01-09-2008, 01:01 PM
  4. Replies: 3
    Last Post: 05-22-2007, 11:42 PM
  5. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM