当我尝试使用 b2Body 的功能时游戏崩溃 - 为什么?
当我尝试在 NSObject
类中使用 b2Body
的功能时,我的应用程序崩溃了。
这是我的类的接口:
#import <Foundation/Foundation.h>
#import "Box2D.h"
#import "GLES-Render.h"
#import "cocos2d.h"
#import "HelloWorldScene.h"
@interface Magnet : NSObject {
b2Body* body;
}
@property (readwrite,assign) b2Body* body;
@end
这是我在 HelloWorldScene.h 中声明它的方式:
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import <UIKit/UIKit.h>
#import "Magnet.h"
@class Magnet;
@interface HelloWorld : CCLayer {
b2World* world;
GLESDebugDraw *m_debugDraw;
Magnet *magnet;
}
这是我在 HelloWorldScene.mm 中访问 magnet
中的 b2Body
的方式:
b2BodyDef bodyDef2;
bodyDef2.type = b2_staticBody;
bodyDef2.position.Set(300/PTM_RATIO, 150/PTM_RATIO);
bodyDef2.userData = sprite2;
magnet.body = world->CreateBody(&bodyDef2);
// Define another box shape for our dynamic body.
//b2PolygonShape dynamicBox;
b2CircleShape dynamicBox2;
//dynamicBox.SetAsBox(1.0f, 1.0f);//These are mid points for our 1m box
dynamicBox2.m_radius = 0.49;
// Define the dynamic body fixture.
b2FixtureDef fixtureDef2;
fixtureDef2.shape = &dynamicBox2;
fixtureDef2.density = 1.3f;
fixtureDef2.friction = 1.0f;
magnet.body->CreateFixture(&fixtureDef2);
我的代码有问题吗?
我在 Box2D 源文件的 CreateFixture()
函数中收到 SIGABRT。我应该怎么办?
抱歉弄乱了,这是回溯:
(gdb) bt
#0 0x0002f35c in b2Body::CreateFixture (this=0x0, def=0xbfffd360) at b2Body.cpp:141
#1 0x00004f7e in -[HelloWorld loadLevel:] (self=0x5544770, _cmd=0xfbc7c, level=1) at HelloWorldScene.mm:255
#2 0x0000573d in -[HelloWorld init] (self=0x5544770, _cmd=0x4ffd774) at HelloWorldScene.mm:140
#3 0x0009d7b1 in +[CCNode node] (self=0x147100, _cmd=0xfc558) at CCNode.m:231
#4 0x0000377d in +[HelloWorld scene] (self=0x147100, _cmd=0xfb61c) at HelloWorldScene.mm:39
#5 0x000036d7 in -[MagnetSpillAppDelegate applicationDidFinishLaunching:] (self=0x680b9f0, _cmd=0xca40a3, application=0x5511370) at MagnetSpillAppDelegate.mm:96
#6 0x008b1ce2 in -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] ()
#7 0x008b3d88 in -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] ()
#8 0x008be617 in -[UIApplication handleEvent:withNewEvent:] ()
#9 0x008b6abf in -[UIApplication sendEvent:] ()
#10 0x008bbf2e in _UIApplicationHandleEvent ()
#11 0x029ca992 in PurpleEventCallback ()
#12 0x0147d944 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#13 0x013ddcf7 in __CFRunLoopDoSource1 ()
#14 0x013daf83 in __CFRunLoopRun ()
#15 0x013da840 in CFRunLoopRunSpecific ()
#16 0x013da761 in CFRunLoopRunInMode ()
#17 0x008b37d2 in -[UIApplication _run] ()
#18 0x008bfc93 in UIApplicationMain ()
#19 0x00002cc4 in main (argc=1, argv=0xbffff058) at main.m:13
My app crashes when I try to use the features of b2Body
from within my NSObject
class.
Here is the interface of my class:
#import <Foundation/Foundation.h>
#import "Box2D.h"
#import "GLES-Render.h"
#import "cocos2d.h"
#import "HelloWorldScene.h"
@interface Magnet : NSObject {
b2Body* body;
}
@property (readwrite,assign) b2Body* body;
@end
And here is how I declare it in HelloWorldScene.h:
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import <UIKit/UIKit.h>
#import "Magnet.h"
@class Magnet;
@interface HelloWorld : CCLayer {
b2World* world;
GLESDebugDraw *m_debugDraw;
Magnet *magnet;
}
And here is how I access the b2Body
in magnet
, in HelloWorldScene.mm:
b2BodyDef bodyDef2;
bodyDef2.type = b2_staticBody;
bodyDef2.position.Set(300/PTM_RATIO, 150/PTM_RATIO);
bodyDef2.userData = sprite2;
magnet.body = world->CreateBody(&bodyDef2);
// Define another box shape for our dynamic body.
//b2PolygonShape dynamicBox;
b2CircleShape dynamicBox2;
//dynamicBox.SetAsBox(1.0f, 1.0f);//These are mid points for our 1m box
dynamicBox2.m_radius = 0.49;
// Define the dynamic body fixture.
b2FixtureDef fixtureDef2;
fixtureDef2.shape = &dynamicBox2;
fixtureDef2.density = 1.3f;
fixtureDef2.friction = 1.0f;
magnet.body->CreateFixture(&fixtureDef2);
Is there any problem with my code?
I'm receiving a SIGABRT in the Box2D source files, in the CreateFixture()
function. What should I do?
Sorry about the mess, here is the backtrace:
(gdb) bt
#0 0x0002f35c in b2Body::CreateFixture (this=0x0, def=0xbfffd360) at b2Body.cpp:141
#1 0x00004f7e in -[HelloWorld loadLevel:] (self=0x5544770, _cmd=0xfbc7c, level=1) at HelloWorldScene.mm:255
#2 0x0000573d in -[HelloWorld init] (self=0x5544770, _cmd=0x4ffd774) at HelloWorldScene.mm:140
#3 0x0009d7b1 in +[CCNode node] (self=0x147100, _cmd=0xfc558) at CCNode.m:231
#4 0x0000377d in +[HelloWorld scene] (self=0x147100, _cmd=0xfb61c) at HelloWorldScene.mm:39
#5 0x000036d7 in -[MagnetSpillAppDelegate applicationDidFinishLaunching:] (self=0x680b9f0, _cmd=0xca40a3, application=0x5511370) at MagnetSpillAppDelegate.mm:96
#6 0x008b1ce2 in -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] ()
#7 0x008b3d88 in -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] ()
#8 0x008be617 in -[UIApplication handleEvent:withNewEvent:] ()
#9 0x008b6abf in -[UIApplication sendEvent:] ()
#10 0x008bbf2e in _UIApplicationHandleEvent ()
#11 0x029ca992 in PurpleEventCallback ()
#12 0x0147d944 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#13 0x013ddcf7 in __CFRunLoopDoSource1 ()
#14 0x013daf83 in __CFRunLoopRun ()
#15 0x013da840 in CFRunLoopRunSpecific ()
#16 0x013da761 in CFRunLoopRunInMode ()
#17 0x008b37d2 in -[UIApplication _run] ()
#18 0x008bfc93 in UIApplicationMain ()
#19 0x00002cc4 in main (argc=1, argv=0xbffff058) at main.m:13
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有这次事故,很难说具体出了什么问题。
然而,该代码充满了崩溃的可能性。
您正在使用堆栈上的结构,而不是分配的结构。如果在完成这些结构之前该代码的范围就消失了,则会导致崩溃和未定义的行为。
您没有初始化这些结构中的所有字段;其余值将处于未定义状态。由于无法区分未初始化的
float32
和已初始化的float32
,因此最终必然会导致问题。您正在堆栈上设置引用堆栈上其他结构的结构;第一点的另一种形式,但应该被指出,因为它使泄漏堆栈指针变得更加容易。
b2Body::CreateFixture() 的第 141 行是一个断言:
你的世界被锁定了吗?看起来不应该。
Without the crash, it is hard to say specifically what is going wrong.
However, that code is rife with potential for crashes.
you are using structures that are on the stack, not allocated. If the scope of that code goes away before you are done with those structures, it'll lead to crashes and undefined behavior.
you aren't initializing all the fields in those structures; the remaining values will be in an undefined state. Since it is impossible to differentiate an uninitialized
float32
from an initializedfloat32
, that is bound to eventually lead to problems.you are setting up structures on the stack that have references to other structures on the stack; another form of the first point, but should be called out because it makes it doubly easy to leak pointers to the stack.
Line 141 of b2Body::CreateFixture() is an assert:
Is your world locked? Looks like it shouldn't be.