Sign in to follow this  
vlasko

delete causes assertion

Recommended Posts

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[i].VertexArray!=NULL) { delete []KeyFrames[i].VertexArray; KeyFrames[i].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 ???

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Looks like you are deleting something that is already deleted.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
so i found the problem(hate those strings) changing filename from char* to const char* solves it.My question is when will be this array deleted(if)-i cannot use delete []filename in destructor because of const modifier

Share this post


Link to post
Share on other sites
Quote:
Original post by vlasko
i cannot use delete []filename in destructor because of const modifier


Yes you can.

However, you can't have a const array if you're going to change the contents of it. So, I assume you are making the mistake of not copying the filename into the char array (as it's const you're not allowed to and you've not mentioned that). You're probably assigning the pointer, which means you're not pointing to your array of 80 chars anymore but are pointing to the string "x.md2". If you call delete on this then of course you'll have a problem. Are you using strncpy or something similar?

If the char array is always going to be of size 80, why are you dynamically creating it? You might as well have a member char FileName[80]; then you don't have to call delete.

Anyway, I'd recommend using std::string.

Share this post


Link to post
Share on other sites

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