Jump to content
  • Advertisement
Sign in to follow this  
vlasko

delete causes assertion

This topic is 5037 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 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 ???

Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!