Debug Assertion Failed! Why?!

Started by
3 comments, last by furiousuk 20 years, 4 months ago
I''ve just created a short program that displays tiles onto the screen. The main function takes a pointer to an array which contains an id of the texture to display at each location. The texture manager then uses the id to set the active texture before drawing each tile to the screen. It all works fine (which kind of surprised me) BUT when I close the window I get this error: "Debug Assertion Failed! Program: blah blah File: dbgheap.c line: 1011 Expression: _CrtIsValidHeapPointer(pUserData)" If I try to debug I get a breakpoint error. Here are my class declarations: // TileData.h // // Contains two classes, the TileData class which is a container class for each tile''s data and // the TileRenderer class which takes a pointer to an array of TileData''s and renders to the // screen a portion of the array. #pragma comment(lib, "MirusLib.lib") #include "Mirus\Mirus.h" #define NORTH 0 #define EAST 1 #define SOUTH 2 #define WEST 3 #define MAX_X 6 #define MAX_Y 6 #define SIZE 64 #define MAX_TEXTURES 3 class TileData { public: int m_iTexture; int m_iOrientation; // (0=N, 1=E, 2=S, 3=W) }; class TileMap { public: TileData m_kTiles[MAX_X * MAX_Y]; }; class TextureManager { public: mrTexture m_kTextures[MAX_TEXTURES]; TextureManager(void); ~TextureManager(void); void Create(void); }; class TileRenderer { protected: TileMap *m_paTileMap; TextureManager *m_pTextureManager; public: TileRenderer(void); ~TileRenderer(void); void Create(TileMap *paTileMap, TextureManager *m_paTextures); void Render(); // For now, render the whole array }; I know my texture file (mrTexture) works fine, its taken from a book and works in every other app I''ve got. In the destructor for TileRenderer I delete the memory for the two pointers and set them to NULL. My knowledge of pointers is pretty limited but I''m guessing the problem has to be in some of my destructors. I''ve let VC++ use default destructors for the other classes. What does this Debug Assertion Failure mean and does it mean I''m leaking memory every time I start the program?
Advertisement
quote:
In the destructor for TileRenderer I delete the memory for the two pointers and set them to NULL. My knowledge of pointers is pretty limited but I''m guessing the problem has to be in some of my destructors. I''ve let VC++ use default destructors for the other classes.

The problem is that you''re not posting any actual code. Debug assertion at the end of your programm could be a double delete, or not deleting at all, but without seeing any ''real'' code it''s hard to tell .
How do I set my laser printer on stun?
The actual code is pretty standard and I thought it would make the post too long.

Anyhow, after messing with the code I''ve found that if I don''t delete the pointers in the destructor then I don''t get this debug error.

I know that you get a CTR debug error when you try to delete a pointer twice, and thats obviously what I''ve done, I just don''t know where I''ve deleted twice - I''ve only called delete in the destructor which is where its supposed to be isn''t it!?!

Thanks for your reply Wildfire but I''ll sleeping dogs lie and just don''t do anything with the pointers in the destructor.
You''ll also get similar debug assertions when you:

- overwrite the end of an array (usually "DAMAGE").

- or use the wrong type of delete e.g. array delete on non-array objects and vice versa.

- or pass invalid pointers to delete.



When you get the assert, follow the instructions it gives you: "(Press Retry to debug the application)" then once you get into the debugger look down the Call Stack window until you get back into YOUR code.

The line in your code that the debugger is indicating in green is the delete call which is at fault. Then it''s up to you to work out why its wrong (usually a logic error).

--
Simon O''Connor
3D Game Programmer &
Microsoft DirectX MVP

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Yeah, I had a similar problem a few years back and it took me ages to figure out what was causing it. Turns out my class had some functions that returned a reference to a local (meaning it was allocated on the stack) object. So basically the destructor of that object deleted the memory associated with it as that method went out of scope and then tried again at some later point as the reference went out of scope as well. DOH! *slaps forehead*

This topic is closed to new replies.

Advertisement