Debug Assertion Failed

This is a discussion on Debug Assertion Failed within the Windows Programming forums, part of the Platform Specific Boards category; I am writing a win32 application and my program compiles and runs. Then when I close it I get a ...

  1. #1
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801

    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.
    Last edited by minesweeper; 12-11-2002 at 11:06 AM.

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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.
    PHP and XML
    Let's talk about SAX

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    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.

  4. #4
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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.
    PHP and XML
    Let's talk about SAX

  5. #5
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    But does this line not mean it has to be deallocated via delete []?

    Code:
    w_coordinates = new int[num_squares];

  6. #6
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    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
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Assertion Failed!
    By IndioDoido in forum C Programming
    Replies: 31
    Last Post: 03-25-2008, 12:07 PM
  2. debug assertion failed!
    By chintugavali in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 04:05 AM
  3. Visual Studio 2005 Debug Assertion Failed
    By natmas in forum C++ Programming
    Replies: 7
    Last Post: 07-17-2007, 05:28 PM
  4. debug assertion failed !
    By blue_gene in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2004, 12:23 PM
  5. debug assertion failed?!?
    By ichijoji in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21