Skip to main content
GameDev.net gamedev.net
🔒 Locked

debug assertion !

Started by gowron67 Dec 13, 2002 at 2:36 PM 5 replies 600+ views
Original Post
gowron67
gowron67
can anyone help with this ? class cImage { cImage(); virtual ~cImage(); ...... unsigned char* mImageData; }; class cTGAImage : public cImage { cTGAImage(); ~cTGAImage( delete mImageData ); ........ }; class cTexture { ~cTexture( delete mImage; } ........ cImage* mImage; }; then in my code...I create the a cTexture object, then create the cImage object and use member functions in the cTGAImage class to load the tga. The tga loads and works fine. My problem is when I try to delete the cTexture object...in the destructor of the cTGAImage, I get a debug assertion when I try to delete mImageData. I don''t understand why this should be, the pointer is valid because I''ve got an ASSERT there.
Sneftel
Sneftel
The code you posted is..... wrong. you''re accessing private variables from derived classes, and apparently making up destructor syntax as you go.

Post the actual code, or there isn''t much we can do to help.


Don''t listen to me. I''ve had too much coffee.
gowron67
gowron67
The way I typed the destructor code was a typo, sorry. Syntax is not the problem otherwise I would have compile errors. I was trying to be concise by pasting the pertinent bits of code. But you're right, I should've pasted the lot...here's the whole code. I hope someone can help.

class cImage
{
public:
cImage();
virtual ~cImage();

virtual bool LoadFile(const char* filename) = 0;
unsigned char* ImageData() { return mImageData; }
unsigned int BPP() { return mBpp; }
unsigned int Width() { return mWidth; }
unsigned int Height() { return mHeight; }
unsigned int Type() { return mType; }

void Type( int type ) { mType = type; }

protected:
unsigned char* mImageData;
unsigned int mBpp; // Image Color Depth In Bits Per Pixel.
unsigned int mWidth; // Image Width
unsigned int mHeight; // Image Height
unsigned int mType; // image type ( 24 / 32 bit )
};

class cTGAImage : public cImage
{
public:
cTGAImage();
~cTGAImage() { delete mImageData; }

bool LoadFile( const char* filename );

/*** for clarity's sake...the LoadFile function is put here...
bool LoadFile( const char* filename )
{
// tried and tested tga loading code....
mImageData = new unsigned char[ imagesize ];
.........
}
***/

bool DeleteFil();

private:
};

class cTexture
{
public:
cTexture();
cTexture( int type );
~cTexture() { delete mImage; }

bool CreateTextureFromData();
bool CreateTextureFromFile( const char* filename ); ( this function calls mImage->LoadFile( filename );

cImage* Image() { return mImage; }

unsigned int ID() { return mID; }

protected:
unsigned int mID;
cImage* mImage;
};

then in the actual game code...

void Loop()
{
test = new cTexture( TGA );
if( !test->CreateTextureFromFile( "test.tga" ) )
gamelog.Print(" could not find file ");

........
}

void CleanUp()
{
delete test;
}



[edited by - gowron67 on December 13, 2002 4:02:05 PM]
Oxyacetylene
Oxyacetylene
I don''t think this is what''s causing the problem but you should replace

delete mImageData;

with

delete[] mImageData;

If you allocate an array then you need to use delete[] rather than delete, or you''ll leak memory. Only use delete if you allocated a single object.
risingdragon3
risingdragon3
Don''t forget that destructors are called in reverse order - ie, first Derived::~Derived, then Base::~Base(). So, make sure you''re not delete[]ing the memory in the cImage destructor.
Otherwise, I can''t really see much else wrong.
gowron67
gowron67
Thanks a lot Oxyacetylene and risingdragon. Both of you were right. I was calling delete mImageData in the base class destructor as well so the memory had already been deleted, and I should''ve used delete [], not delete.....thanks again
Red Ant
Red Ant
But I thought delete [] works both for NULL pointers and non-NULL pointers, except that in the former case it doesn''t actually do anything? :confused:

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.