Sign in to follow this  

Heap errors

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is rather vague and terse, but I'm not really familiar with this sort of thing so hopefully I can get some help. I'm currently working on an application that works with sets of trees that it evaluates, replaces each cycle etc. I've placed a counter in the new/deletes so as far as I can tell, there are no memory leaks. The memory usage on my computer staying nearly constant would indicate the same. Anyway, each 'set' of trees consists of around 400-600 links in a linked list structure. When I add up to around 6 of these sets, everything runs fine and I get the expected results. However, above this number, I get a break/continue error dialog: "Unhandled exception at 0x77f75a58 in KfRd.exe: User breakpoint." at free.c, Line 101: HeapFree(_crtheap, 0, pBlock); Does that mean that something is trying to be illegally free()'d? I'm not sure what to do other than start stepping through thousands of lines of code and scatter printf's everywhere... Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Hmm. Just to narrow things down, you could try a known good implementation (std::list) and create a dummy program which tries to approximate it's memory use (aka, create 7+ lists of 400-600 items).

But I am guessing that you are indeed possibly illegally freeing something. Try this (or similar):

template < typename T > void my_free( T & * ptr )
{
assert( ptr );
free( ptr );
ptr = 0;
}

template < typename T > void my_free( T * ptr )
{
assert( ptr );
free( ptr );
}

#define free(x) my_free((x))


(the 2nd one is provided in case you free by an expression instead of a single pointer).

Share this post


Link to post
Share on other sites
Quote:
Original post by Woodsman
Anyway, each 'set' of trees consists of around 400-600 links in a linked list structure. When I add up to around 6 of these sets, everything runs fine and I get the expected results. However, above this number, I get a break/continue error dialog:
"Unhandled exception at 0x77f75a58 in KfRd.exe: User breakpoint."
at free.c, Line 101: HeapFree(_crtheap, 0, pBlock);

Does that mean that something is trying to be illegally free()'d?

Seems like freeing a block that has already been freed, or a block that wasn't allocated with malloc.

Quote:
I'm not sure what to do other than start stepping through thousands of lines of code and scatter printf's everywhere...

Thanks in advance for any help.

If you're using VC++ you might try _CrtSetDbgFlag.
Using it in a debug build, you can force checking the validity of the heap on every allocation and deallocation, so that you'd know when exactly things went wrong.
Other flags might also be of help (check the linked documentation page).

Heap corruption is generally one of the most difficult bugs to locate.

Share this post


Link to post
Share on other sites
Scatter _CrtCheckMemory() in a few key places until you can narrow down exactly where and when the error appears. Note that it returns TRUE or FALSE, so you need to assert it. It is located in crtdbg.h.

Also consider the _CRTDBG_CHECK_ALWAYS_DF flag to check validity of every allocation and deallocation request. John Robbins "Debugging Applications for .NET and Microsoft Windows" has a ton more power debugging techniques designed for situations like this. Basically, if the debug heap functions can't help him out, he goes through the entire code base and memset's every Win32 structure to 0, initializes every ptr to null if it isn't initialized, and sets every ptr to null after it is free()'d/delete'd.

Share this post


Link to post
Share on other sites

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this