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...