当我尝试使用 b2Body 的功能时游戏崩溃 - 为什么?

发布于 2024-11-07 00:47:34 字数 3007 浏览 0 评论 0原文

当我尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2024-11-14 00:47:34

如果没有这次事故,很难说具体出了什么问题。

然而,该代码充满了崩溃的可能性。

  • 您正在使用堆栈上的结构,而不是分配的结构。如果在完成这些结构之前该代码的范围就消失了,则会导致崩溃和未定义的行为。

  • 您没有初始化这些结构中的所有字段;其余值将处于未定义状态。由于无法区分未初始化的 float32 和已初始化的 float32,因此最终必然会导致问题。

  • 您正在堆栈上设置引用堆栈上其他结构的结构;第一点的另一种形式,但应该被指出,因为它使泄漏堆栈指针变得更加容易。


b2Body::CreateFixture() 的第 141 行是一个断言:

    b2Assert(m_world->IsLocked() == false);

你的世界被锁定了吗?看起来不应该。

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 initialized float32, 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:

    b2Assert(m_world->IsLocked() == false);

Is your world locked? Looks like it shouldn't be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文