• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Strange error pinpointed to be destructor...

## Recommended Posts

Silverbolter    122
Hi, I have a strange error in my program. For some reason, the error is caused by the destructor. Basically, I have 2 classes. One class generates a bomberman grid (BomberMap), and the other class(BomberPixelTrack) will take the generated bomberman grid and create tracks or boundaries which define where the player can walk on. I pass the BomberPixelTrack object the coordinates int x and int y through a function to see if walking there is allowed. The BomberPixelTrack object uses dynamic arrays. Because the number of tracks can vary. However when I run my test driver I get the WRONG result and a debug assertion failed message from MSVC++ 6. However I dont have ASSERT() anywhere. I play around and comment out the "delete [] pointer;" code from my BomberPixelTrack destructor and the ASSERT() message goes away for some reason and I get the RIGHT answer! Can anyone explain whats going on? Here''s my test driver: int main() { std::cout << "First create 7x7 map object\n"; BomberMap mymap = BomberMap(7,7); mymap.GenerateStandardMap(); mymap.PrintMapConsole(); std::cout << "Create BomberPixelTrack object with mymap and tile 64 size\n"; BomberPixelTrack mymaptrack = BomberPixelTrack(mymap,64); std::cout << "Test if track works: Move to (64,64)\n"; if( mymaptrack.OnBomberPixelTrack(64,64)) { std::cout << "On the track\n"; } else { std::cout << "Not on track\n"; } return 0; }

##### Share on other sites
smart_idiot    1298
My guess is you''re writting past the end of an array, and it isn''t detected until you attempt to delete the memory.

int array[5]; // This array contains 5 elements.array[0]; // this is the first.array[4]; // this is the last.array[5]; // this will corrupt memory by writting past the end of the array.

##### Share on other sites
Silverbolter    122

I did a thorough test and that does not seem to be the case.

Microsoft Visual C++ Debug Library (title of box)
Debug Assertion Failed!

Program:...\Testtrack.exe
File:dbgheap.c
Line:1017

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts. (thats what i did but no go)

Does anyone know where I can find dbgheap.c so perhaps I can look into it? I searched for it but didn''t come up. hmmm

My BomberMap object is actually a 2D dynamic array.
I do pretty much the same thing for the destructor (plus a for loop for each row) and it seems to be fine, even if I try to test it with the rest of my engine which uses DirectDraw to blit the map tiles onto screen.

Strange....

##### Share on other sites
Strewth    146
Would help if you showed where the delete is being called...

##### Share on other sites
GamerSg    378
The message basically means u are deleting memory you are not supposed to.

Such errors can be hard to spot, use the debugger and try to find the line which produces this error.

##### Share on other sites
flangazor    516
You are passing an object that has pointers by value.

You do not have a copy constructor, so when you do this, it is implicitly calling the temporary copy of your object''s destructor. That destructor is deleting the memory pointed to by the pointers in your original object. When you delete your original object, it tries to delete memory already deleted.

Solution: Write a proper copy constructor (where the array is copied entirely -known as a "deep copy") or pass by const reference (I suggest this one).

##### Share on other sites
Silverbolter    122
Bang on! The deep copy constructor did it! Thanks a lot, I had a misconception that the copy constructor is only required if you want to initialize a new object from an existing one. Now I know it is actually used by the computer automatically sometimes and I''ll be sure to define it just like in class haha THANKS.

I''m only aware of how to do deep copies, what exactly does pass by const reference do? Does this mean just to copy the references? Then woudlnt 2 objects have members that point to the same data and if you delete one it wont work?

Thanks.

##### Share on other sites
Thunder_Hawk    314
If you pass by const reference, then the copy constructor will never be called and the object''s destructor will not be called at the end of the function (which should be the expected behaviour). In other words, passing by const reference will avoid your problem, but please do not rely on yourself always passing that way .

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________