Game Development Community

Possible memory leak in AStar?

by Richard Skala · in iTorque 2D · 02/13/2010 (12:00 am) · 2 replies

I had a bug where an enemy would be created outside of a path grid, which would cause pathGrid2d::createPath() to be called repeatedly, and then caused invalid paths to be repeatedly created. Eventually, the game would crash because of this.

When pathGrid2d::createPath() is called, it allocates a new 'pathAStar2d' each time. I noticed that if the 'pathAStar2d' is invalid, there is only a call to unregisterObject(), and not a 'delete' on the created pathAStar2d. I am not that familiar with the Torque code base as of yet, but could this be a possible problem and/or memory leak?

I am referring to C++ code, by the way, not script, and the code I refer to is in pathGrid2d.cc.


#1
02/13/2010 (4:10 am)
I'm fairly certain there IS a memory leak in the case I mentioned.

After digging a little bit further, and it seems to me that unregisterObject() is not the correct function to use inside createPath() after the 'isValidPath' check fails, as I don't see where the object is deleted.

Someone please correct me if I am wrong, but it seems that when creating a path fails,

instead of:

pPathObj->unregisterObject();

it should be either:

pPathObj->safeDelete();

or

pPathObj->deleteObject();


It should be noted that safeDelete() calls deleteObject(), and deleteObject() calls unregisterObject() and actually deallocates memory for the object (uses 'delete').
#2
02/13/2010 (10:13 am)
thats very well possible that there are problems with the path

the path object always was flacky, there have been dublication issues etc in the past in TGB and alike.