Thread: "delete []" in destructor causes assertion failure

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    47

    "delete []" in destructor causes assertion failure

    I have a class, whose constructor makes a public pointer point to an array (allocated with "new" by a protected member function) of another class. When I attempt to delete this array from the destructor, I get an assertion failure in "dbgdel.cpp" - aka the code behind the "delete" operator (right?). When I don't delete the array, I (of course) am left with a memory leak. So I'd like to get this to work. Here's the code ("//..." indicates omissions of code that seems to be irrelevant):

    Code:
    class path_coord
    {public: unsigned int x, y; path_coord(){x = -1; y = -1;}}; // -1 "wraps" to maximum value
    //...
    
    class pathroute
    {
    
    protected:
    	mazemaker * map;
    
    public:
    
    	path_coord * path;
    	unsigned long length;
    
    	pathroute(unsigned int start_x, unsigned int start_y, unsigned int dest_x, unsigned int dest_y, unsigned int map_width, unsigned int map_height, mazemaker * ptrMap)
    	{
    		map = ptrMap;
    		path = AStar(start_x, start_y, dest_x, dest_y, map_width, map_height);
    	}
    
    	~pathroute()
    	{
    		delete [] path;
    	}
    protected:
    	//...
    	path_coord * createReturn(node * end)
    	{
    
    		node * currentnode = end;
    		unsigned long numnodes = 1;
    		while(currentnode != NULL)
    		{
    			numnodes++;
    			currentnode = currentnode->parent;
    		}
    
    		path_coord * ret = new path_coord[numnodes];
    
    		currentnode = end;
    		unsigned long currentindex = 0;
    		while(currentnode != NULL)
    		{
    			ret[currentindex].x = currentnode -> x;
    			ret[currentindex].y = currentnode -> y;
    			currentindex++;
    			currentnode = currentnode->parent;
    		}
    
    		length = numnodes - 1;
    		return ret;
    
    	}
    	//...
    };
    Can anybody tell me what I'm doing wrong? Thanks in advance for any help...

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    EDIT: I realize one thing that wasn't clear in the previous post is that createReturn is called by AStar, and AStar returns whatever createReturn returned, after cleaning up, like so:

    Code:
    path_coord * ret;
    //...
    ret = createReturn(makenode); //Makenode is a pointer to the last node that was made and put in the open list.
    deletelist(open);
    deletelist(closed);
    return ret;

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    provide your own assignment operator and copy constructor because you are using dynamic memory the compiler supplied functions will cause problems.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    Thanks, I just did a test, and that does seem to be the problem. I have a global function (for debugging purposes) that, given a pathroute object and a filename, dumps the contents of the path array into that file. That appears to be the thing that had been giving me problems. By changing it's pathroute argument to a reference (thereby removing the need to copy the object) I eliminated the error. This doesn't solve the problem, really; I still will need to be able to copy and assign pathroutes, but at least now I know what the problem is. So now I, like you said, need to write my own operators. Thanks for pointing me in the right direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 02:22 AM
  3. debug assertion failure
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2004, 11:23 AM
  4. Assertion failure while creating window
    By roktsyntst in forum Windows Programming
    Replies: 0
    Last Post: 02-10-2003, 08:18 PM
  5. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 07:02 PM