Mattsa008 / pybox2d

Automatically exported from code.google.com/p/pybox2d
Other
0 stars 0 forks source link

Contact listener refcount not increased with SetContactListener #24

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago

See the bottom for the punchline first.  Everything in the middle is just
context fyi:

What steps will reproduce the problem?

1. world.SetContactListener(b2ContactListener())
2. run a simulation that causes a contact

What is the expected output? What do you see instead?

gdb of python catches a segfault as follows:

(gdb) where
#0  0xb756518f in b2PolyAndCircleContact::Evaluate (this=0x836b574,
listener=0x832fe58) at Box2D/Dynamics/Contacts/b2PolyAndCircleContact.cpp:122
#1  0xb7564cf4 in b2Contact::Update (this=0x836b574, listener=0x832fe58) at
Box2D/Dynamics/Contacts/b2Contact.cpp:153
#2  0xb755ec01 in b2World::SolveTOI (this=0x8313db8, step=@0xbfdb42ac) at
Box2D/Dynamics/b2World.cpp:651
#3  0xb755f18b in b2World::Step (this=0x8313db8, dt=0.0166666675,
velocityIterations=20, positionIterations=16) at Box2D/Dynamics/b2World.cpp:878
#4  0xb75500a2 in _wrap_b2World_Step (args=0xb7db5a54) at
Box2D/Box2D_wrap.cpp:27850
#5  0xb54765c8 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) list
117                 b2Vec2 v2 = b2->GetLinearVelocityFromLocalPoint(mp->localPoint2);
118                 cp.velocity = v2 - v1;
119                 cp.normal = m_manifold.normal;
120                 cp.separation = mp->separation;
121                 cp.id = id;
122                 listener->Add(&cp);
123             }
124         }
125 
126         m_manifoldCount = 1;
(gdb) print listener->Add
$5 = {void (b2ContactListener *, const b2ContactPoint *)} 0xb7559b20
<b2ContactListener::Add(b2ContactPoint const*)>
(gdb) print listener
$6 = (b2ContactListener *) 0x832fe58
(gdb) print *listener
$7 = {_vptr.b2ContactListener = 0x1}
(gdb) print *0x832fe58
$8 = 1

What version of the product are you using? On what operating system?

Ubuntu Hardy, version 2.0.2b1

Please provide any additional information below.

No other problems.  The simulation runs fine until what I assume is the
first contact, and then dies as above.  If I never call SetContactListener,
the sim runs fine indefinitely.  I have tried with my own class (subclass
of b2ContactListener) and just passing in a naked b2ContactListener as above.

Ok -- I just figured out what the problem is:

SetContactListener does not increment the reference count to the passed
object, so if you don't store it, it gets GC'd and causes a segfault.  Easy
to fix, I assume?

Original issue reported on code.google.com by brandyn....@gmail.com on 14 Sep 2009 at 1:56

GoogleCodeExporter commented 8 years ago
(Sorry, I would change the title to reflect the actual bug, but don't see how 
to do
that here.)

Original comment by brandyn....@gmail.com on 14 Sep 2009 at 1:58

GoogleCodeExporter commented 8 years ago
It'll require a separate SetContactListener in the way I've done the userdata 
(see
[http://code.google.com/p/pybox2d/source/browse/trunk/Box2D/Box2D_userdata.i 
here])
-- Ignoring the original, using a custom __del__ (with a decref) in b2World so 
that
objects containing its reference will destruct properly.

Same applies to debug draw, boundary, and destruction listeners.

I've always known about the issue, it was just never high on the list as its 
fix is
simple -- store it. :) Thanks for the report in any case; I'll probably get 
around to
it eventually.

Original comment by sir...@gmail.com on 14 Sep 2009 at 2:14

GoogleCodeExporter commented 8 years ago
Yup, easy workaround.  A quick "fix" would be to just document the bug.
Would this be the best place?
http://www.box2d.org/wiki/index.php?title=Python_Manual#Contact_Listener
(I don't seem to have edit access; not sure what it takes to create an account 
there
and all that -- if it's easy for you...)  And/or I suppose in the doc string for
SetContactListener.
All in all, pybox2d is working great btw...

Original comment by brandyn....@gmail.com on 14 Sep 2009 at 4:18

GoogleCodeExporter commented 8 years ago
Fixed (or at least an attempted fix) in the bleeding edge 2.1.0 version. Don't 
intend
on fixing this for 2.0.2.

Original comment by sir...@gmail.com on 11 Jan 2010 at 2:38