• Advertisement

Archived

This topic is now archived and is closed to further replies.

Debug Assertion failed message

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

hi All, I am stumped as to why i get "Debug Assertion failed" message when i am in Debug mode, yet in Release mode i get no errors! THe exact error message is this:
  


---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

Program: C:\source\OGL\Debug\OGL.exe
File: dbgheap.c
Line: 1011

Expression: _CrtIsValidHeapPointer(pUserData)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
---------------------------
Abort   Retry   Ignore   
---------------------------

  
OGL.exe is my application which caused the error. After debugging, I noticed the error occurs when i free the pointer of type float. This is the exact point where the error occurs (the free() function call):
  
	if (Scene->objlist[n].NormalFaceArray)
				free (Scene->objlist[n].NormalFaceArray);
  
The problem is I have defined NormalFaceArray as a pointer of type float and stepping through the program the address is null (0x00000). (which is correct) So why is it still going through the free() call? This error doesnt occur in release mode, yet i get it in debug mode! Any pointers as to what mistake I have made? thanks. Xtreme. Edited by - Xtreme on February 4, 2002 5:14:14 AM

Share this post


Link to post
Share on other sites
Advertisement
Are you sure that the error occurs with a NULL pointer, since NULL is valid for free anyway. Put a breakpoint on the line with the free and have a closer look.
The cause that you don''t get the message with a release build is that the release version of the runtime doesn''t check the heap.

Share this post


Link to post
Share on other sites
quote:
Original post by Xtreme
yes,
i have tried it by assigning NULL to it, and it tries to
free it!


Can''t be,
but to be sure, just write

  
if( Scene->objlist[n].NormalFaceArray != NULL )
{
free (Scene->objlist[n].NormalFaceArray);
}


Make SURE you don''t have a stray semicolon at the end of that if-line.
If the pointer is null, you will not enter the if-block. If it enters the if-block, the pointer was NOT null.


People might not remember what you said, or what you did, but they will always remember how you made them feel.

Share this post


Link to post
Share on other sites
VolkerG, you can not pass NULL to "free ()". I think it may be safe to pass NULL to "delete" though.

- Pete

Share this post


Link to post
Share on other sites
ok, this is wat i got now:

  

---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!

Program: C:\source\OGL\Debug\OGL.exe
File: dbgheap.c
Line: 1017

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------




The expression is a bit different but still i get this!

Share this post


Link to post
Share on other sites
Just to show you that i has assigned NULL to it:

    

OGLObject * OGL_3dsloader::GetObject()
{
void * mem;
uint size = sizeof(OGLObject)*(Scene->objects+1);

if((mem=realloc(Scene->objlist, size)) == 0) {
sprintf(emsg, "Error reallocating memory for object list "
"(%u bytes).", size);
throw emsg;
}

Scene->objlist=(OGLObject *) mem;
OGLObject * obj=&Scene->objlist[Scene->objects++];
memset(obj, 0, sizeof(OGLObject));
Scene->objlist->ColourArray = NULL;
Scene->objlist->NormalArray = NULL;
Scene->objlist->NormalFaceArray = NULL;
Scene->objlist->TextureArray = NULL;
Scene->objlist->VertexArray = NULL;

return obj;
}



This is the object structure defintion:

  

typedef struct OGLObject
{
ushort faces; /* number of faces in facelist */
ushort vertices; /* number of vertices in vtxlist */
uchar color; /* mesh color index [0...63] */
char name[16]; /* object name, zero terminated */
OGLFace * faclist;
OGLVertex *vtxlist;

float *VertexArray; /* To be used with glVertexPointer */
float *TextureArray; /* To be used with glTexCoordPointer */
float *ColourArray; /* To be used with glColorPointer */
//float *NormalVertexArray;/* To be used with glNormalPointer */
float *NormalFaceArray; /* To be used with glNormalPointer */

float *NormalArray;


} OGLObject;



and this is my memory deallocation part:

  
void OGL_3dsloader::OGLFreeScene()
{
if(Scene) {
if(Scene->objlist) {
for(int n=0; n<Scene->objects; n++) {
if(Scene->objlist[n].faclist)
free(Scene->objlist[n].faclist);
if(Scene->objlist[n].vtxlist)
free(Scene->objlist[n].vtxlist);
}
if (Scene->objlist[n].VertexArray)
free (Scene->objlist[n].VertexArray);
//if (Scene->objlist[n].NormalVertexArray)

// free (Scene->objlist[n].NormalVertexArray);

if (Scene->objlist[n].NormalFaceArray)
free (Scene->objlist[n].NormalFaceArray);
if (Scene->objlist[n].NormalArray)
free (Scene->objlist[n].NormalArray);
if (Scene->objlist[n].TextureArray)
free (Scene->objlist[n].TextureArray);
if (Scene->objlist[n].ColourArray)
free (Scene->objlist[n].ColourArray);
free(Scene->objlist);
}
if (Scene->matlist)
free(Scene->matlist);
free(Scene);
Scene = NULL;
}
}




Edited by - Xtreme on February 4, 2002 6:52:38 AM

Share this post


Link to post
Share on other sites
to siaspete from the MSDN Library:
Remarks
...
If memblock is NULL, the pointer is ignored and free immediately returns.
...
And to Xtreme: why is in the OGL_3dsloader::OGLFreeScene method the block beginning with VertexArray after the closing brace of the for loop? Here you are adressing with n pointing behind the end of your array, aren't you?

Edited by - VolkerG on February 4, 2002 7:38:46 AM

Share this post


Link to post
Share on other sites
Volker, i dont quite understand your question but basically
the "n" is the object structure count and that structure
is part of a bigger structure, ie,
OGLObject * objlist; /* array of objects */

so i have to go through all of the objects and delete them
anyway, problem doesnt occur there, it fails it the
if (Scene->objlist[n].NormalFaceArray)
free (Scene->objlist[n].NormalFaceArray);

point!

Share this post


Link to post
Share on other sites
quote:
Original post by Xtreme
so i have to go through all of the objects and delete them
anyway, problem doesnt occur there, it fails it the
if (Scene->objlist[n].NormalFaceArray)
free (Scene->objlist[n].NormalFaceArray);

point!


No. Volker is right. If that''s your exact code, you will be attempting to free a block that is in fact past memory that you have allocated. In the code causing the problem, n is equal to Scene->objects, meaning you are accessing one element beyond the end of the array. You have to put the frees WITHIN the for loop.




People might not remember what you said, or what you did, but they will always remember how you made them feel.

Share this post


Link to post
Share on other sites
VolkerG, many implementations of the standard library will crash on freeing a NULL pointer. It works fine in MSVC, but it''s still bad practice to do it as the results aren''t guaranteed across compilers.

Share this post


Link to post
Share on other sites
Thought we''re talking about tracking the error. And in this case the free (NULL) couldn''t be the cause of the error since Xtreme seems to use the VC runtimes.

Share this post


Link to post
Share on other sites
the code i would like to see; how are you assigning "Scene->objlist[n].NormalFaceArray"?

the reason i ask, is because the number one reasons for getting this error is a double "free" call.

i.e.

  
free(Scene->objlist[n].NormalFaceArray);
free(Scene->objlist[n].NormalFaceArray);


or trying to free something not created on the heap.

i.e.

  
int array[10];

int main(void)
{
free(array);
return (0);
}


both those options will produce the EXACT same error you have reached. even the wording is exact.


if you can, please post code showing how you assign the array pointers.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
I found out the problem!!! THanks to all of you expecially VolkerG and MadKeithV.

The for loop was not going around the NormalArray, VertexArray, etc. I mistakenly put the curly brackets before it instead of after it! So it should be like this:

    

void OGL_3dsloader::OGLFreeScene()
{
if(Scene) {
if(Scene->objlist) {
for(int n=0; n<Scene->objects; n++) {
if(Scene->objlist[n].faclist)
free(Scene->objlist[n].faclist);
if(Scene->objlist[n].vtxlist)
free(Scene->objlist[n].vtxlist);

if (Scene->objlist[n].VertexArray)
free (Scene->objlist[n].VertexArray);
//if (Scene->objlist[n].NormalVertexArray)

// free (Scene->objlist[n].NormalVertexArray);

if (Scene->objlist[n].NormalFaceArray)
free (Scene->objlist[n].NormalFaceArray);
if (Scene->objlist[n].NormalArray)
free (Scene->objlist[n].NormalArray);
if (Scene->objlist[n].TextureArray)
free (Scene->objlist[n].TextureArray);
if (Scene->objlist[n].ColourArray)
free (Scene->objlist[n].ColourArray);
}
free(Scene->objlist);
}
if (Scene->matlist)
free(Scene->matlist);
free(Scene);
Scene = NULL;
}
}



Silly me!
Thats what happens if you work on your code at nights!


Edited by - Xtreme on February 4, 2002 6:47:15 PM

Share this post


Link to post
Share on other sites

  • Advertisement