Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Debugging Memory Corruption

Author
Message
Michael P
19
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 19th Jul 2011 01:17 Edited at: 19th Jul 2011 01:18
So I'm working on a project written by someone else which is too big to debug without the help of some tools. I have to use visual studio 2005 and I'm getting memory corruption where an attempt is made to free a resource after its already been freed. So I imagine something a bit like this is happening:


The problem is that the in built debugger doesn't help much at all... It only tells me something's wrong when the block that was freed twice is later allocated again (I think this is what's happening anyway).

So I'm hoping someone can recommend some tools that can point me to the line where the delete which is doing the damage is located? Or am I going to have to just do it manually?

Many thanks to anyone who can help me!

Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 19th Jul 2011 01:23 Edited at: 19th Jul 2011 01:23
You can override the "new" and "delete" operators with your own functions which keep track of the memory. In the delete operator check if the memory has already been freed, and break into the debugger if that is the case. Then you can look at the stack trace to see where the error is.

There are loads of articles on the internet explaining the details of how to do it.

[b]
Michael P
19
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 19th Jul 2011 01:30
Ah thank you, will let you know how this goes! I started off doing this but then I thought it may not be possible because I couldn't imagine what the syntax would look like. I thought use a macro (#define)?

Anyway hopefully this is straightforward, will look into it.

Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 19th Jul 2011 01:31
There's no need for a macro. Just write replacement new and delete operators in the global scope.

[b]
Michael P
19
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 19th Jul 2011 21:18
It was quite easy to do . I have a set containing the address of all memory allocated with the new operator. The delete operator removes from this set, and if a delete operator trys to remove an address not in the set then it breaks out so I can look at the stack trace. Haven't tried on the full system yet but have it working on test examples. Cheers!

Login to post a reply

Server time is: 2025-05-21 03:47:06
Your offset time is: 2025-05-21 03:47:06