Skip to main content
GameDev.net gamedev.net
🔒 Locked

Save call stack within object...

Started by TechnoCore Mar 17, 2005 at 2:36 PM 4 replies 950+ views
Original Post
TechnoCore
TechnoCore
I was just thinking about something to help me when debugging. When one of my objects or whatever crashes it can sometimes be hard to find out which object it was that actually crashed. I would like to add a class that saves the location where in the code the object that crashed was created. (I.e add this class to every object i suspect) It would get hold of the call-stack from within the constructor of a class and store this information, or something like that. So when the object dies i can just check which object it was that actually died. Are there tools for this? Otherwise it would be nice to make such a class, and use it together with a special assert that points out the location of the objects creation within the code. But i'm not sure how to do it. Maybe im not making sense at all ;) Thank you! //T
//TechnoCore
doynax
doynax
You can use the StackWalk API to traverse the call stack.
Unfortunately the information about it bit scarce. Atleast this wrapper library should provide you with a decent starting point.
Toolmaker
Toolmaker
What IDE/Compiler are you using? Visual Studio comes with a stacktracer, in which you can see the stack. Just find it in the debug menu.

Toolmaker
 
markr
markr
Can you not configure your operating system / C library / whatever to create a post-mortem crash dump file that you can later load into a debugger and view the stack trace?

I've no idea how to do this on win32/NT, but I have a feeling that that nice Dr. Watson is involved somehow.

Mark
Palidine
Palidine
Quote:
Original post by TechnoCore
Are there tools for this?


Isn't the Debugger such a tool? Run the code through the debugger...when it crashes you have a callstack and everything you need to solve the problem.

-me
iMalc
iMalc
None of the above posters seem to have read the question properly. He wants to know where the object was allocated, not where the crash is.

What about making a class that contains an internal list of pointers and file and line numbers. e.g.:
#define TRACK_ALLOCATION(x) myTracker.addItem(__FILE__,__LINE__,(void*)(x))class tracker{  //some std::list of structs, with file, line, and ptr info goes here  public:    void addItem(const char *file, unsigned int, void *ptr)    { //...    }    void showCreationPosition(void *ptr)    { //...    }} myTracker;
The difficulty is now in calling showCreationPosition with the right ptr value when you get an error. Oh and you probably want to know what called the function that allocated the memory etc - DOH!
That's a bit much info I reckon, cancel that idea.

Maybe you'd be better off just making sure that your program has lots of appropriate asserts in it!?!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.