Sign in to follow this  

A Unknown Problem in the Heap

This topic is 3934 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

I have a question i cant seem to solve it, i am doing some old time programming and creating my windows (screens) through API. I am using the Visual Studio 2005 but i am not programming .net. I always get the error that my heap is corrupted. I have a engine class, a screen class but also a screencontroller class. The screencontroller class contains a basic static wndclassex and is registers only once, upon destruction it unregister my window. Problem is the following i have a static function called register_screen this is to track how many windows are currently open. When i try to create a single linked list i get the follow error that my heap is corrupt Code looks like this because i delete everything just for testing ----------------------------------------------------------------- //GETS CALLED 2 TIMES bool scrcontroller::register_screen() { int *p p = new int; //SECOND TIME CRASH return false; } This could be normal because you would think that i forgot to delete my p after i was done. Problem is also when i do this. struct wnd_listitem { wnd_listitem * next; }; static wnd_listitem * wnd_list; //This is how it is declared in the header file wnd_listitem * scrcontroller :: wnd_list = NULL; // This is declared in cpp file bool scrcontroller::register_screen() //STATIC FUNCTION { if(wnd_list) { wnd_list->next = new wnd_listitem;//ERROR HEAP CORRUPTED AFTER 2de ENTRY } else { wnd_list = new wnd_listitem; //SUCCESS NO PROBLEMS 1st Time } return false; }

Share this post


Link to post
Share on other sites
Something in your code somewhere is writing past the end of an array, or writing to a pointer after it's been deleted. I'd recommend getting a memory manager like Fluid Studios' mmgr.

Share this post


Link to post
Share on other sites
Quote:
Original post by Joran
I have a question i cant seem to solve it, i am doing some old time programming and creating my windows (screens) through API. I am using the Visual Studio 2005 but i am not programming .net. I always get the error that my heap is corrupted.


If you're running in debug mode, can you get a call stack? If so, what does the stack look like? Is it failing during the allocation, or at some point further down the road?

Quote:
Original post by Joran
//GETS CALLED 2 TIMES
bool scrcontroller::register_screen()
{

int *p
p = new int; //SECOND TIME CRASH

return false;
}


At the very least, this is a memory leak as you're not storing p anywhere.

Quote:
Original post by Joran
This could be normal because you would think that i forgot to delete my p after
i was done. Problem is also when i do this.


If you're deleting it at the end of the call, why not just make it a local and not a pointer?

Quote:
Original post by Joran
static wnd_listitem * wnd_list; //This is how it is declared in the header file
wnd_listitem * scrcontroller :: wnd_list = NULL; // This is declared in cpp file

bool scrcontroller::register_screen() //STATIC FUNCTION
{
if(wnd_list)
{
wnd_list->next = new wnd_listitem;//ERROR HEAP CORRUPTED AFTER 2de ENTRY
}
else
{
wnd_list = new wnd_listitem; //SUCCESS NO PROBLEMS 1st Time
}

return false;

}


Have you considered using the STL linked list instead? If you're concerned about template bloat, you could wrap it inside of a custom class so that STL was only used internally....

I only ask because there's a pretty serious bug/leak in the code above. As written, the code will only ever create a list of two items - the initial wnd_list pointer and its next element. Each time you add a new item, you're just overwriting the old next pointer, creating a link *and* making the old item inaccessible (assuming, that is, that you haven't stored a pointer to it elsewhere).

Are you setting the next pointer to null on construction of a new wnd_listitem?

Share this post


Link to post
Share on other sites

This topic is 3934 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