#26 destructors reference invalid pointers, segfault

open
nobody
None
8
2012-09-14
2006-09-27
Chris Bainbridge
No

With reference to the pyode thread here
http://sourceforge.net/mailarchive/forum.php?thread_id=30160006&forum_id=38876
and the problem outlined by Matthias Baas in the 5th email.

There are multiple destructors (for each object plus
dWorldDestroy, joint group destructors) within ode.
Some objects hold pointers to others that are used at
destructor time (eg. the Geom destructor tries to
unlink itself from its associated body, without
checking that the pointer is still valid).

This results in the situation where it isn't clear
which objects are still valid, and which have already
been free'd, so calling various destructors results in
memory problems which can cause segfaults (also visible
with valgrind).

This is particularly a problem where the destructor
order is non-deterministic, as in pythons garbage
collection, as there is no way from within a pyode
program to force deallocation of specific ode objects.

At the moment I hack around this by explicitly
destroying pointers between ode objects on simulation
close by deattaching all joints and geoms from bodies, ie:

for j in self.joints:
j.attach(None, None)
for g in self.space:
if g.placeable():
g.setBody(None)

I assume that this is also a problem with non-python
wrappers as well. Ode needs fixing so that the
ownership of objects for destruction is clear, and that
destructors don't reference pointers to other attached
objects that may already have been deallocated.

Discussion

  • Jason Perkins
    Jason Perkins
    2006-09-27

    Logged In: YES
    user_id=1683

    Just to confirm - this is for the C++ version of the API,
    right?

     
  • Logged In: YES
    user_id=428581

    Yes. The problem just becomes more obvious when using
    wrappers for another language.

     
  • Bram Stolk
    Bram Stolk
    2006-12-26

    Logged In: YES
    user_id=14028
    Originator: NO

    I think all bindings, C, C++, Python suffer from the same root problem:
    ownership is unclear, and the wrong order of destruction can cause a crash.
    We should really fix this.