0
\$\begingroup\$

I have two functions that are virtually the same with a few small differences in code, one to create a static body and one to create a dynamic body, however if I call both of them it results in a segmentation fault, but I'm not quite sure why as far as I know I'm not trying to redefine any objects (other than bbox which is a box2d * body).

I've tried debugging it it appears the issue is caused by b2FixtureDef but I again Don't know exactly what it is, From looking at the memory somewhere there is an allocation issue as 0xfeeefeee shows up in a dump of the registers.

as long as I only use one its fine but when I use two or more it crashes. if someone could point me in the right direction ( the code is valid 0 errors and 0 warnings so I'm not looking for handouts )

void entity::setAsPlatform(float hx, float hy, b2World world)
{
   b2BodyDef platformDef;
   platformDef.position.Set(bbox_pos.x,bbox_pos.y);
   b2Body * platform = world.CreateBody(&platformDef);
   b2PolygonShape platformShape;
   platformShape.SetAsBox(hx, hy);
   b2FixtureDef platformFixture;
   platformFixture.shape = &platformShape;
   platform->CreateFixture(&platformFixture);
   bbox = platform;
}


void entity::setAsPlayer(float hx, float hy, b2World world)
{
   b2BodyDef platformDef;
   platformDef.position.Set(bbox_pos.x,bbox_pos.y);
   platformDef.type = b2_dynamicBody;
   b2Body * platform = world.CreateBody(&platformDef);
   b2PolygonShape platformShape;
   platformShape.SetAsBox(hx, hy);
   b2FixtureDef platformFixture;
   platformFixture.shape = &platformShape;
   platform->CreateFixture(&platformFixture);
   bbox = platform;
 }

and in main.cpp

entity platform("Platform", 20, 500);
platform.setAsPlatform(2.0f, 50.0f, world);

entity player("Player", 0, 20);
player.setAsPlayer(1.0f,1.0f,world);
\$\endgroup\$
1
  • 3
    \$\begingroup\$ This question appears to be off-topic because it is about debugging your code for you. \$\endgroup\$ Commented Sep 20, 2013 at 2:44

1 Answer 1

1
\$\begingroup\$

Your main problem is that you keep copying and destroying values that you don't mean to destroy.

Let's take for example world

When you call platform.setAsPlatform(2.0f, 50.0f, world); you make a copy of world because setAsPlatform takes a b2World by value.

You probably meant to write

void entity::setAsPlayer(float hx, float hy, b2World &world)

This would take world per reference and not make a copy which is destroyed at the end of the function. So you added your objects to a copy of your world that you then destroyed, so when the object then tries to look up anything in the world that it's in it'll access already freed up memory as the 0xfeeefeee indicates.

\$\endgroup\$
5
  • \$\begingroup\$ heh seems I'm still making novice mistakes thanks this issues been bugging me for a few hours now if I could up vote I would, so I've been copying where I should have been passing by reference?, if thats the case no wonder I've been having so many issues. \$\endgroup\$
    – Xecantur
    Commented Sep 20, 2013 at 2:53
  • \$\begingroup\$ @Xecantur you can pass it as a pointer or as a reference but if you pass by value it will act as if it is an independent value. You have to express your intend correctly. You wanted to pass the instance of the world that you already created to the function, you didn't just want to pass a value equivalent to the world you wanted to pass this specific world. \$\endgroup\$
    – PeterT
    Commented Sep 20, 2013 at 3:25
  • \$\begingroup\$ @Xecantur added another case where you didn't anticipate the lifetime of your objects. You don't really seem to be aware of the semantic difference of locals (usually on the stack) and dynamically allocated memory (usually on the heap). \$\endgroup\$
    – PeterT
    Commented Sep 20, 2013 at 3:40
  • \$\begingroup\$ @Xecantur never mind, the edit I added was wrong, If you read the documentation it states "The shape, this must be set. The shape will be cloned, so you can create the shape on the stack.". It is worth noting that this API is not idiomatic C++, so it's important to read the documentation. \$\endgroup\$
    – PeterT
    Commented Sep 20, 2013 at 3:49
  • \$\begingroup\$ thanks, now all I got to do is work out the issue of drawing the platforms on top of the bounding boxes, having an issue where the box precedes the sprite. \$\endgroup\$
    – Xecantur
    Commented Sep 20, 2013 at 4:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .