-
Debug Assertion Failed
I am writing a win32 application and my program compiles and runs. Then when I close it I get a Debug Assertion Failed Message.
The expression is:
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
My program has the following classes:
Code:
class zone;
class waypoints
{
private:
int *w_coordinates;
int number_of_w_squares;
zone *zone1;
zone *zone2;
public:
void set_w_coordinates(int pos_x, int pos_y, int a);
void setup(int num_squares, zone &a, zone &b);
zone* GetZone1();
zone* GetZone2();
int GetCoordinate0();
int GetCoordinate1();
~waypoints();
}w_1,w_2,w_3,w_4;
void waypoints::setup(int num_squares, zone &a, zone &b)
{
number_of_w_squares = num_squares;
w_coordinates = new int[num_squares];
zone1 = &a;
zone2 = &b;
}
void waypoints::set_w_coordinates(int x, int y, int b)
{
b = b*2;
w_coordinates[b] = x;
w_coordinates[b+1] = y;
}
waypoints::~waypoints()
{
delete [] w_coordinates;
w_coordinates = NULL;
}
zone* waypoints::GetZone1()
{
return zone1;
}
zone* waypoints::GetZone2()
{
return zone2;
}
int waypoints::GetCoordinate0()
{
return w_coordinates[0];
}
int waypoints::GetCoordinate1()
{
return w_coordinates[1];
}
class zone
{
private:
int number_of_squares, number_of_interfaces;
int *coordinates;
public:
vector <waypoints> interfaces;
void setup(int a, int b);
void set_coordinates(int i, int pos_x, int pos_y);
void set_interfaces(waypoints *a);
bool IsInZone(int x, int y);
int GetSquaresNumber();
int GetSquareCoordinate(int i);
int GetNumberOfInterfaces();
~zone();
}zone_main_corridor,zone_adjoining_corridor_lstairs,zone_adjoining_corridor_rstairs,zone_stairs,zone_offices_corridor;
void zone::setup(int a, int b)
{
number_of_squares = a;
coordinates = new int[a];
number_of_interfaces = b;
}
zone::~zone()
{
delete [] coordinates;
coordinates = NULL;
interfaces.erase(interfaces.begin(),interfaces.end());
}
void zone::set_coordinates(int i, int pos_x, int pos_y)
{
coordinates[i] = pos_x;
coordinates[i+1] = pos_y;
}
void zone::set_interfaces(waypoints *a)
{
interfaces.push_back(*a);
}
bool zone::IsInZone(int x, int y)
{
for (int i=0; i<number_of_squares; i++)
{
if (coordinates[i*2] == x && coordinates[(i*2)+1] == y)
{
return TRUE;
}
}
return FALSE;
}
int zone::GetSquaresNumber()
{
return number_of_squares;
}
int zone::GetSquareCoordinate(int i)
{
return coordinates[i];
}
int zone::GetNumberOfInterfaces()
{
return number_of_interfaces;
}
I have managed to narrow it down to the following lines of code that are causing the problem. I did this by commenting everything out and then bringing code back in till the error occured.
Code:
zone_main_corridor.set_interfaces(&w_1);
zone_adjoining_corridor_lstairs.set_interfaces(&w_1);
zone_adjoining_corridor_lstairs.set_interfaces(&w_2);
zone_stairs.set_interfaces(&w_2);
zone_stairs.set_interfaces(&w_3);
zone_adjoining_corridor_rstairs.set_interfaces(&w_3);
zone_adjoining_corridor_rstairs.set_interfaces(&w_4);
zone_offices_corridor.set_interfaces(&w_4);
I don't understand why the message is occuring. I have made sure that I am calling the destructor on all instances of zone and waypoints on program closure.
-
this is usually the error that means you are calling delete on a variable that wasn't allocated with new...as far as i can tell there is no calls to new in your program. If it uses new, then delete, if not, do nothing. If that's not the problem then let me know.
-
After some more investigating it would seem that the problem occurs when the destructor is called on the zones. However I don't know any more than this because when I use the debugger and try and step into the destructor it gives me the error message.
-
Are you dull or something??
did you not read what i posted earlier?? heres your problem:
you're creating this pointer:
int * w_coordinates;
then you call delete [] on it
theres 2 problems with this, first of all, you didn't allocate memory with it with new, so don't call delete! second, you're calling delete array, and even if you had used new on that int, it wouldn't be an array. So do as i say and take out your calls to delete.
-
But does this line not mean it has to be deallocated via delete []?
Code:
w_coordinates = new int[num_squares];
-
ah, didn't see that before...but that error does happen when you delete a pointer that wasn't allocated.....
try using new when you declare it, not later...may help, i don't know what to do without the full source