this thing is making me crazy!
I’m experimenting with Cocos2d (0.99.5) and Box2D, and I have successfully created a scene with a layer and two bodies (actually a couple of bouncing balls). Now I would like to enable the DebugDrawing so I can see exactly what is happening. Consider that everything works with DebugDrawing disabled.
Talking about the code, in my init method I have this:
m_debugDraw = new GLESDebugDraw(PTM_RATIO);
uint32 flags;
flags = 0;
flags += 1 * b2DebugDraw::e_shapeBit;
flags += 1 * b2DebugDraw::e_jointBit;
flags += 1 * b2DebugDraw::e_aabbBit;
flags += 1 * b2DebugDraw::e_pairBit;
flags += 1 * b2DebugDraw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
_world->SetDebugDraw(m_debugDraw);
My draw method is as follows:
-(void)draw {
[super draw];
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
_world->DrawDebugData(); // <------ here comes the problem
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
The problem is that when the program reaches the line
_world->DrawDebugData();
an EXC_BAD_ACCESS exception in thrown. The debugger shows the error here:
void b2World::DrawDebugData()
{
if (m_debugDraw == NULL)
{
return;
}
uint32 flags = m_debugDraw->GetFlags(); // <----- this is the row pointed by Xcode
if (flags & b2DebugDraw::e_shapeBit)
{
[...]
The error shown on the main window is: “Thread 1: Program received signal: ‘EXC_BAD_ACCESS'”. This is the output I get:
GNU gdb 6.3.50-20050815 (Apple version gdb-1515) (Sat Jan 8 00:31:48 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “–host=x86_64-apple-darwin –target=arm-apple-darwin”.tty /dev/ttys000
target remote-mobile /tmp/.XcodeGDBRemote-191-40Switching to remote-macosx protocol
mem 0x1000 0x3fffffff cache
mem 0x40000000 0xffffffff none
mem 0x00000000 0x0fff none
[Switching to thread 11779]
[Switching to thread 11779]
sharedlibrary apply-load-rules all
2011-03-03 22:10:36.477 MyApp[2738:707] cocos2d: cocos2d v0.99.5
2011-03-03 22:10:36.529 MyApp[2738:707] cocos2d: Using Director Type:CCDirectorDisplayLink
2011-03-03 22:10:36.936 MyApp[2738:707] cocos2d: OS version: 4.3 (0x04030000)
2011-03-03 22:10:36.943 MyApp[2738:707] cocos2d: GL_VENDOR: Imagination Technologies
2011-03-03 22:10:36.949 MyApp[2738:707] cocos2d: GL_RENDERER: PowerVR SGX 535
2011-03-03 22:10:36.956 MyApp[2738:707] cocos2d: GL_VERSION: OpenGL ES-CM 1.1 IMGSGX535-58.1
2011-03-03 22:10:36.966 MyApp[2738:707] cocos2d: GL_MAX_TEXTURE_SIZE: 2048
2011-03-03 22:10:36.973 MyApp[2738:707] cocos2d: GL_MAX_MODELVIEW_STACK_DEPTH: 16
2011-03-03 22:10:36.979 MyApp[2738:707] cocos2d: GL_MAX_SAMPLES: 4
2011-03-03 22:10:37.020 MyApp[2738:707] cocos2d: GL supports PVRTC: YES
2011-03-03 22:10:37.026 MyApp[2738:707] cocos2d: GL supports BGRA8888 textures: YES
2011-03-03 22:10:37.033 MyApp[2738:707] cocos2d: GL supports NPOT textures: YES
2011-03-03 22:10:37.039 MyApp[2738:707] cocos2d: GL supports discard_framebuffer: YES
2011-03-03 22:10:37.060 MyApp[2738:707] cocos2d: compiled with NPOT support: NO
2011-03-03 22:10:37.066 MyApp[2738:707] cocos2d: compiled with VBO support in TextureAtlas : YES
2011-03-03 22:10:37.072 MyApp[2738:707] cocos2d: compiled with Affine Matrix transformation in CCNode : YES
2011-03-03 22:10:37.078 MyApp[2738:707] cocos2d: compiled with Profiling Support: NO
2011-03-03 22:10:44.375 MyApp[2738:707] cocos2d: Frame interval: 1
2011-03-03 22:10:44.420 MyApp[2738:707] cocos2d: surface size: 320×480
2011-03-03 22:10:44.654 MyApp[2738:707] Received memory warning. Level=1
2011-03-03 22:10:44.696 MyApp[2738:707] cocos2d: deallocing
"MenuBackGround.png", "Star.png", "fps_images.png")>
Current language: auto; currently c++
(gdb)
I have tried googling and I found out a lot of things about this error, still I can’t solve it. It happens both on the simulator and on the iPhone. Someone on another discussion (sorry, lost the link) pointed out that it may be related to the compiler or the optimization, thus I tried different combinations switching from LLVM to GCC to LLVM-GCC and setting the optimization at different levels with no success.
Can someone point me in the right direction? Let me know if more information is needed.
Thanks you all,
Daniele Salatti
The only difference to the Cocos2D Box2D template is that you are calling SetDebugDraw after assigning the flags. It shouldn’t make any difference but you might want to try it exactly like the Box2D template project does:
Update: I found another difference. In your draw method, you’re calling [super draw]. The Box2D template project doesn’t do that. Again, it should not make any difference but you never know. If it still crashes I recommend to compare your project with the Box2D template to find any other (possibly subtle) differences. In addition to the usual recommendations to exclude all compiler hiccups: cleaning all targets, deleting build folder, restarting Xcode (and entire machine), deleting the App from Simulator/Device (and rebooting device).
PS: I see you’re using underscore prefixes as in: _world. Apple recommends not to do that since underscore prefixes are reserved for their own internal use, as well as some C style functions. In ObjC, if you must name member variables according to some scheme, prefer to use underscore suffixes as in world_->SetDebugDraw()
Personally I try to avoid these world, world or m_world naming schemes altogether, there’s really no point in that other than tradition and conflicting function parameter names. Where necessary, I rather change the function parameter names to have a suffixed underscore.