delete causes assertion

Started by
11 comments, last by Qw3r7yU10p! 19 years, 6 months ago
I want to create a md2 loader so I've created a class md2_model as follows class md2_model{ char *FileName; int numSkins; int numVertices; int numTexCoords; int numTriangles; int numGlCommands; int numFrames; struct md2_frame* KeyFrames; struct md2_triangle* TrianglesArray; struct md2_skin* SkinNames; struct md2_texCoord* TexCoordArray; struct md2_frame deltaFrame; void CalculateNormals(int KeyFrameNumber); public: bool LoadMD2Model(char* filen); void ShowMD2Model(); md2_model(); ~md2_model(); }; md2_model::md2_model() { FileName=new char[80]; KeyFrames=0; SkinNames=0; TexCoordArray=0; TrianglesArray=0; } md2_model::~md2_model() { int i; for(i=0;i<numFrames;i++) { if(KeyFrames.VertexArray!=NULL) { delete []KeyFrames.VertexArray; KeyFrames.VertexArray=NULL; } } if(KeyFrames!=NULL) { delete []KeyFrames; KeyFrames=NULL; } if(SkinNames!=NULL) { delete []SkinNames; SkinNames=NULL; } if(TexCoordArray!=NULL) { delete []TexCoordArray; TexCoordArray=NULL; } if(TrianglesArray!=NULL) { delete []TrianglesArray; TrianglesArray=NULL; } if(FileName!=NULL) { delete []FileName; FileName=NULL; } } I'm using a simple program for testing if it=s working correctly int main(int argc, char *argv[]) { object1=new md2_model("x.md2"); if(object1->LoadMD2Data()==false) { delete object1; return 1; } cout<<"OK"; delete object1; return 0; } THe problem is that by calling delete object1 destructor finished ok but then i get assertion error - i've checked it and object1 has an adress.what is the problem then ???
Advertisement
You probably should be checking for exceptions. New throws bad_alloc on an error.
If new was throwing, then you wouldn't see the destructor called, because the program wouldn't get there.

You don't say what the assert actually says -- you should make sure to write it down in detail, if you want good help. My guess is that it's the MSVCRT debug mode heap checking claiming that your heap is corrupted, because you've written outside some allocated chunk of memory.

You can add this assert at the beginning and end of each of your functions:

  assert( _CrtCheckMemory() != 0 );


This will make it stop as soon as the error is found, although it will make the debug-mode build of your program slightly slower. To use this function, you also need towards the top of your file:

#include <assert.h>extern "C" int _CrtCheckMemory();

enum Bool { True, False, FileNotFound };
use std::vector instead of arrays. It's far less error prone than managing the memory yourself.

[Edited by - petewood on October 1, 2004 1:30:46 PM]
Is numFrames being set to something other than garbage?

Also, the first thing delete does is check whether it's being called on a null pointer, so you don't need to have the checks in there.
This is what it says:
File:dbgheap.c
Line:1011

Expression: _CrtIsValidHeapPointer(pUserData)

Yes numFrames is loaded from file in LoadMD2model()
Will try assert to see whats wrong
Looks like you are deleting something that is already deleted.
Quote:Original post by vlasko
This is what it says:
File:dbgheap.c
Line:1011

Expression: _CrtIsValidHeapPointer(pUserData)

Yes numFrames is loaded from file in LoadMD2model()
Will try assert to see whats wrong

Two possibilities here, 1) you deleted something that was already deleted, as the AP said, or 2) You overwrote an array bound somewhere and skrewed up the bookkeeping data that the C allocator uses.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

DUDE! MessageBox after each delete, and see where the ACTUAL problem is.
Personally I prefer looking at the callstack in the debugger than coding MessageBoxes... you get more detailed information and it's less work.

This topic is closed to new replies.

Advertisement