I've got a really annoying problem, and because lack of experience I really need help on this one.
Box2D: 2.0.2 (from svn: branches/groundzero rev. 228)
System: Arch Linux 2.6.30.5-1
Compiler: gcc 4.4.1-1
I'm working on a SameGame where I need the contact points to find all connected marbles. My program crashes when I'm using world->SetContactListener() in combination with body->CreateShape(). It will crash in the world->step() function! If I comment any of the lines out, the program wont crash, nor will it work as I want!
This is the first time I'm using Box2D and everything worked fine until I added the following code:
GameEngine.hpp
GameEngine.cpp
I've also tried to use an external object (created a separate class called MyListener and istanciated it from GameEngine) without success.
All marbles spawns at the same position (0,0), even if I set their positions before the bodies are created in the world. And the program will crash immediately (because everything is in collision). If I only spawn one marble, it will fall by the gravity to the ground body, and at collision the program crashes.
This is how I create the bodies:
Debugger output while running:
Stacktrace:
valgrind --leak-check=yes ./SameGame
Is there something I missed, or does Box2D behave unexpected?
Box2D: 2.0.2 (from svn: branches/groundzero rev. 228)
System: Arch Linux 2.6.30.5-1
Compiler: gcc 4.4.1-1
I'm working on a SameGame where I need the contact points to find all connected marbles. My program crashes when I'm using world->SetContactListener() in combination with body->CreateShape(). It will crash in the world->step() function! If I comment any of the lines out, the program wont crash, nor will it work as I want!
This is the first time I'm using Box2D and everything worked fine until I added the following code:
GameEngine.hpp
Code:
class GameEngine : public b2ContactListener {
private:
...
b2World* world;
public:
...
virtual void Add(const b2ContactPoint* point) {
// handle add point
}
virtual void Persist(const b2ContactPoint* point) {
// handle persist point
}
virtual void Remove(const b2ContactPoint* point) {
// handle remove point
}
virtual void Result(const b2ContactResult* point) {
// handle results
}
};
private:
...
b2World* world;
public:
...
virtual void Add(const b2ContactPoint* point) {
// handle add point
}
virtual void Persist(const b2ContactPoint* point) {
// handle persist point
}
virtual void Remove(const b2ContactPoint* point) {
// handle remove point
}
virtual void Result(const b2ContactResult* point) {
// handle results
}
};
GameEngine.cpp
Code:
...
world = new b2World(worldAABB, gravity, doSleep);
world->SetContactListener(this);
...
world = new b2World(worldAABB, gravity, doSleep);
world->SetContactListener(this);
...
I've also tried to use an external object (created a separate class called MyListener and istanciated it from GameEngine) without success.
All marbles spawns at the same position (0,0), even if I set their positions before the bodies are created in the world. And the program will crash immediately (because everything is in collision). If I only spawn one marble, it will fall by the gravity to the ground body, and at collision the program crashes.
This is how I create the bodies:
Code:
b2BodyDef bodyDef;
bodyDef.position.Set(positionX, positionY);
bodyDef.angle = rotation;
bodyDef.allowSleep = true;
this->body = world->CreateBody(&bodyDef);
b2CircleDef shapeDef;
shapeDef.radius = radius;
shapeDef.density = 1.0f;
shapeDef.friction = 0.5f;
shapeDef.restitution = 0.5f;
this->body->CreateShape(&shapeDef);
this->body->SetMassFromShapes();
bodyDef.position.Set(positionX, positionY);
bodyDef.angle = rotation;
bodyDef.allowSleep = true;
this->body = world->CreateBody(&bodyDef);
b2CircleDef shapeDef;
shapeDef.radius = radius;
shapeDef.density = 1.0f;
shapeDef.friction = 0.5f;
shapeDef.restitution = 0.5f;
this->body->CreateShape(&shapeDef);
this->body->SetMassFromShapes();
Debugger output while running:
Code:
...
Debugger name and version: GNU gdb 6.8
Child process PID: 4315
Program received signal SIGSEGV, Segmentation fault.
In ?? () ()
Debugger name and version: GNU gdb 6.8
Child process PID: 4315
Program received signal SIGSEGV, Segmentation fault.
In ?? () ()
Stacktrace:
Code:
#0 ( ??() (??:??)
#1 0xb7fd97b4 b2CircleContact::Evaluate(this=0x9f592ec, listener=0xbfc2dc9c) (Box2D/Dynamics/Contacts/b2CircleContact.cpp:84)
#2 0xb7fd88cd b2Contact::Update(this=0x9f592ec, listener=0xbfc2dc9c) (Box2D/Dynamics/Contacts/b2Contact.cpp:153)
#3 0xb7fd8373 b2ContactManager::Collide(this=0x9c87a1c) (Box2D/Dynamics/b2ContactManager.cpp:235)
#4 0xb7fd8108 b2World::Step(this=0x9c6e840, dt=0.0166666675, velocityIterations=10, positionIterations=8) (Box2D/Dynamics/b2World.cpp:867)
#5 0x804e1b9 engine::GameEngine::Run(this=0xbfc2dc98) (/SameGame/GameEngine.cpp:105)
#6 0x8051932 main() (/SameGame/main.cpp:5)
#1 0xb7fd97b4 b2CircleContact::Evaluate(this=0x9f592ec, listener=0xbfc2dc9c) (Box2D/Dynamics/Contacts/b2CircleContact.cpp:84)
#2 0xb7fd88cd b2Contact::Update(this=0x9f592ec, listener=0xbfc2dc9c) (Box2D/Dynamics/Contacts/b2Contact.cpp:153)
#3 0xb7fd8373 b2ContactManager::Collide(this=0x9c87a1c) (Box2D/Dynamics/b2ContactManager.cpp:235)
#4 0xb7fd8108 b2World::Step(this=0x9c6e840, dt=0.0166666675, velocityIterations=10, positionIterations=8) (Box2D/Dynamics/b2World.cpp:867)
#5 0x804e1b9 engine::GameEngine::Run(this=0xbfc2dc98) (/SameGame/GameEngine.cpp:105)
#6 0x8051932 main() (/SameGame/main.cpp:5)
valgrind --leak-check=yes ./SameGame
Code:
==3210== Memcheck, a memory error detector.
==3210== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==3210== Using LibVEX rev 1884, a library for dynamic binary translation.
==3210== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==3210== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==3210== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==3210== For more details, rerun with: -v
==3210==
==3210== My PID = 3210, parent PID = 3164. Prog and args are:
==3210== ./SameGame
==3210==
==3210== Jump to the invalid address stated on the next line
==3210== at 0x0: ???
==3210== by 0x40558CC: b2Contact::Update(b2ContactListener*) (b2Contact.cpp:153)
==3210== by 0x4055372: b2ContactManager::Collide() (b2ContactManager.cpp:235)
==3210== by 0x4055107: b2World::Step(float, int, int) (b2World.cpp:867)
==3210== by 0x804E1B8: engine::GameEngine::Run() (GameEngine.cpp:105)
==3210== by 0x8051931: main (main.cpp:5)
==3210== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==3210==
==3210== Process terminating with default action of signal 11 (SIGSEGV)
==3210== Bad permissions for mapped region at address 0x0
==3210== at 0x0: ???
==3210== by 0x40558CC: b2Contact::Update(b2ContactListener*) (b2Contact.cpp:153)
==3210== by 0x4055372: b2ContactManager::Collide() (b2ContactManager.cpp:235)
==3210== by 0x4055107: b2World::Step(float, int, int) (b2World.cpp:867)
==3210== by 0x804E1B8: engine::GameEngine::Run() (GameEngine.cpp:105)
==3210== by 0x8051931: main (main.cpp:5)
==3210==
==3210== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 105 from 2)
==3210== malloc/free: in use at exit: 20,336,837 bytes in 3,711 blocks.
==3210== malloc/free: 9,563 allocs, 5,852 frees, 36,939,425 bytes allocated.
==3210== For counts of detected errors, rerun with: -v
==3210== searching for pointers to 3,711 not-freed blocks.
==3210== checked 9,579,896 bytes.
==3210==
==3210==
==3210== 59 bytes in 3 blocks are possibly lost in loss record 56 of 158
==3210== at 0x4024B9D: operator new(unsigned int) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3210== by 0x412E1FF: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.12)
==3210== by 0x412F000: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/libstdc++.so.6.0.12)
==3210== by 0x412F1E5: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.12)
==3210== by 0x4091288: ui::UserInterface::UserInterface(engine::GameListener&, int, int, bool) (UserInterface.cpp:6)
==3210== by 0x804D852: engine::GameEngine::GameEngine(int, int, bool) (GameEngine.cpp:4)
==3210== by 0x8051925: main (main.cpp:4)
==3210==
==3210==
==3210== 3,524 (196 direct, 3,328 indirect) bytes in 1 blocks are definitely lost in loss record 118 of 158
==3210== at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3210== by 0x4F19D0C: _mesa_malloc (in /usr/lib/xorg/modules/dri/libdricore.so)
==3210== by 0x4E07A10: driConcatConfigs (in /usr/lib/xorg/modules/dri/i965_dri.so)
==3210== by 0x4E1E919: intelInitScreen2 (in /usr/lib/xorg/modules/dri/i965_dri.so)
==3210== by 0x4E09818: dri2CreateNewScreen (in /usr/lib/xorg/modules/dri/i965_dri.so)
==3210== by 0x465DBD9: dri2CreateScreen (in /usr/lib/libGL.so.1.2)
==3210== by 0x463A222: __glXInitialize (in /usr/lib/libGL.so.1.2)
==3210== by 0x4635D39: GetGLXPrivScreenConfig (in /usr/lib/libGL.so.1.2)
==3210== by 0x4636A47: glXGetConfig (in /usr/lib/libGL.so.1.2)
==3210== by 0x444A8E6: sf::priv::WindowImplX11::CreateContext(sf::VideoMode const&, XVisualInfo&, sf::WindowSettings&, XVisualInfo, unsigned long) (in /usr/lib/libsfml-window.so.1.5)
==3210== by 0x444AEC1: sf::priv::WindowImplX11::WindowImplX11() (in /usr/lib/libsfml-window.so.1.5)
==3210== by 0x44475BC: sf::priv::WindowImpl::New() (in /usr/lib/libsfml-window.so.1.5)
==3210==
==3210==
==3210== 139,372 bytes in 12 blocks are possibly lost in loss record 152 of 158
==3210== at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3210== by 0x40639AA: b2Alloc(int) (b2Settings.cpp:33)
==3210== by 0x40637D6: b2BlockAllocator::Allocate(int) (b2BlockAllocator.cpp:128)
==3210== by 0x4056479: b2CircleContact::Create(b2Shape*, b2Shape*, b2BlockAllocator*) (b2CircleContact.cpp:29)
==3210== by 0x40556C3: b2ContactManager::PairAdded(void*, void*) (b2ContactManager.cpp:54)
==3210== by 0x4069165: b2PairManager::Commit() (b2PairManager.cpp:318)
==3210== by 0x406C3D5: b2BroadPhase::CreateProxy(b2AABB const&, void*) (b2BroadPhase.cpp:311)
==3210== by 0x4065917: b2Shape::CreateProxy(b2BroadPhase*, b2XForm const&) (b2Shape.cpp:116)
==3210== by 0x4051194: b2Body::CreateShape(b2ShapeDef*) (b2Body.cpp:195)
==3210== by 0x80516D0: Marble::GenerateShape() (Marble.cpp:16)
==3210== by 0x8051650: Marble::Marble(b2World*, float, float, float, float, sf::Color) (Marble.cpp:6)
==3210== by 0x804DA33: engine::GameEngine::GenerateMarbles() (GameEngine.cpp:18)
==3210==
==3210== LEAK SUMMARY:
==3210== definitely lost: 196 bytes in 1 blocks.
==3210== indirectly lost: 3,328 bytes in 16 blocks.
==3210== possibly lost: 139,431 bytes in 15 blocks.
==3210== still reachable: 20,193,882 bytes in 3,679 blocks.
==3210== suppressed: 0 bytes in 0 blocks.
==3210== Reachable blocks (those to which a pointer was found) are not shown.
==3210== To see them, rerun with: --leak-check=full --show-reachable=yes
==3210== Copyright (C) 2002-2008, and GNU GPL'd, by Julian Seward et al.
==3210== Using LibVEX rev 1884, a library for dynamic binary translation.
==3210== Copyright (C) 2004-2008, and GNU GPL'd, by OpenWorks LLP.
==3210== Using valgrind-3.4.1, a dynamic binary instrumentation framework.
==3210== Copyright (C) 2000-2008, and GNU GPL'd, by Julian Seward et al.
==3210== For more details, rerun with: -v
==3210==
==3210== My PID = 3210, parent PID = 3164. Prog and args are:
==3210== ./SameGame
==3210==
==3210== Jump to the invalid address stated on the next line
==3210== at 0x0: ???
==3210== by 0x40558CC: b2Contact::Update(b2ContactListener*) (b2Contact.cpp:153)
==3210== by 0x4055372: b2ContactManager::Collide() (b2ContactManager.cpp:235)
==3210== by 0x4055107: b2World::Step(float, int, int) (b2World.cpp:867)
==3210== by 0x804E1B8: engine::GameEngine::Run() (GameEngine.cpp:105)
==3210== by 0x8051931: main (main.cpp:5)
==3210== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==3210==
==3210== Process terminating with default action of signal 11 (SIGSEGV)
==3210== Bad permissions for mapped region at address 0x0
==3210== at 0x0: ???
==3210== by 0x40558CC: b2Contact::Update(b2ContactListener*) (b2Contact.cpp:153)
==3210== by 0x4055372: b2ContactManager::Collide() (b2ContactManager.cpp:235)
==3210== by 0x4055107: b2World::Step(float, int, int) (b2World.cpp:867)
==3210== by 0x804E1B8: engine::GameEngine::Run() (GameEngine.cpp:105)
==3210== by 0x8051931: main (main.cpp:5)
==3210==
==3210== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 105 from 2)
==3210== malloc/free: in use at exit: 20,336,837 bytes in 3,711 blocks.
==3210== malloc/free: 9,563 allocs, 5,852 frees, 36,939,425 bytes allocated.
==3210== For counts of detected errors, rerun with: -v
==3210== searching for pointers to 3,711 not-freed blocks.
==3210== checked 9,579,896 bytes.
==3210==
==3210==
==3210== 59 bytes in 3 blocks are possibly lost in loss record 56 of 158
==3210== at 0x4024B9D: operator new(unsigned int) (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3210== by 0x412E1FF: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.12)
==3210== by 0x412F000: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/libstdc++.so.6.0.12)
==3210== by 0x412F1E5: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/libstdc++.so.6.0.12)
==3210== by 0x4091288: ui::UserInterface::UserInterface(engine::GameListener&, int, int, bool) (UserInterface.cpp:6)
==3210== by 0x804D852: engine::GameEngine::GameEngine(int, int, bool) (GameEngine.cpp:4)
==3210== by 0x8051925: main (main.cpp:4)
==3210==
==3210==
==3210== 3,524 (196 direct, 3,328 indirect) bytes in 1 blocks are definitely lost in loss record 118 of 158
==3210== at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3210== by 0x4F19D0C: _mesa_malloc (in /usr/lib/xorg/modules/dri/libdricore.so)
==3210== by 0x4E07A10: driConcatConfigs (in /usr/lib/xorg/modules/dri/i965_dri.so)
==3210== by 0x4E1E919: intelInitScreen2 (in /usr/lib/xorg/modules/dri/i965_dri.so)
==3210== by 0x4E09818: dri2CreateNewScreen (in /usr/lib/xorg/modules/dri/i965_dri.so)
==3210== by 0x465DBD9: dri2CreateScreen (in /usr/lib/libGL.so.1.2)
==3210== by 0x463A222: __glXInitialize (in /usr/lib/libGL.so.1.2)
==3210== by 0x4635D39: GetGLXPrivScreenConfig (in /usr/lib/libGL.so.1.2)
==3210== by 0x4636A47: glXGetConfig (in /usr/lib/libGL.so.1.2)
==3210== by 0x444A8E6: sf::priv::WindowImplX11::CreateContext(sf::VideoMode const&, XVisualInfo&, sf::WindowSettings&, XVisualInfo, unsigned long) (in /usr/lib/libsfml-window.so.1.5)
==3210== by 0x444AEC1: sf::priv::WindowImplX11::WindowImplX11() (in /usr/lib/libsfml-window.so.1.5)
==3210== by 0x44475BC: sf::priv::WindowImpl::New() (in /usr/lib/libsfml-window.so.1.5)
==3210==
==3210==
==3210== 139,372 bytes in 12 blocks are possibly lost in loss record 152 of 158
==3210== at 0x402522D: malloc (in /usr/lib/valgrind/x86-linux/vgpreload_memcheck.so)
==3210== by 0x40639AA: b2Alloc(int) (b2Settings.cpp:33)
==3210== by 0x40637D6: b2BlockAllocator::Allocate(int) (b2BlockAllocator.cpp:128)
==3210== by 0x4056479: b2CircleContact::Create(b2Shape*, b2Shape*, b2BlockAllocator*) (b2CircleContact.cpp:29)
==3210== by 0x40556C3: b2ContactManager::PairAdded(void*, void*) (b2ContactManager.cpp:54)
==3210== by 0x4069165: b2PairManager::Commit() (b2PairManager.cpp:318)
==3210== by 0x406C3D5: b2BroadPhase::CreateProxy(b2AABB const&, void*) (b2BroadPhase.cpp:311)
==3210== by 0x4065917: b2Shape::CreateProxy(b2BroadPhase*, b2XForm const&) (b2Shape.cpp:116)
==3210== by 0x4051194: b2Body::CreateShape(b2ShapeDef*) (b2Body.cpp:195)
==3210== by 0x80516D0: Marble::GenerateShape() (Marble.cpp:16)
==3210== by 0x8051650: Marble::Marble(b2World*, float, float, float, float, sf::Color) (Marble.cpp:6)
==3210== by 0x804DA33: engine::GameEngine::GenerateMarbles() (GameEngine.cpp:18)
==3210==
==3210== LEAK SUMMARY:
==3210== definitely lost: 196 bytes in 1 blocks.
==3210== indirectly lost: 3,328 bytes in 16 blocks.
==3210== possibly lost: 139,431 bytes in 15 blocks.
==3210== still reachable: 20,193,882 bytes in 3,679 blocks.
==3210== suppressed: 0 bytes in 0 blocks.
==3210== Reachable blocks (those to which a pointer was found) are not shown.
==3210== To see them, rerun with: --leak-check=full --show-reachable=yes
Is there something I missed, or does Box2D behave unexpected?