1
\$\begingroup\$

I want to know how to release/clear/remove a variable. (specifically b2BodyDef, b2Body, b2FixtureDef, b2PolygonShape types.)

I've heard there's a keyword called delete, guessing by it's name it may have something to do with this subject?


I'm totally new to these things and I've just heard that there's a way to do this.

I might be wrong and there isn't a way to do this at all.

Which also means that I have no idea if I should include code or what information I should include at all :P

\$\endgroup\$
1
  • \$\begingroup\$ delete is the c++ operator you use to free up any memory you allocated with the new operator. I have no experience with Box2D, but this is most likely not what you are looking for. I think the method suggested by 11mad11 suffices for freeing the memory as well; Box2D will most likely do this for you, though it may not even be allocating this using new. I suggest reading how (dynamic) memory allocaiton in c++ works. \$\endgroup\$
    – Athos vk
    Dec 18 '15 at 20:40
1
\$\begingroup\$

i dont know if its the same for c++ but in java its

world.destroyBody(body);

and after in java we do body = null;
in c++ do like this : http://www.cplusplus.com/reference/cstdlib/free/ to free all memory that we dont need anymore.

\$\endgroup\$
7
  • \$\begingroup\$ It exists in c++ too, this only removes the body from the world though, how would I go about clearing/releasing the variables? \$\endgroup\$
    – BiiX
    Dec 18 '15 at 20:13
  • \$\begingroup\$ in java for release a variable we use body = null; \$\endgroup\$
    – 11mad11
    Dec 18 '15 at 20:15
  • \$\begingroup\$ cplusplus.com/reference/cstdlib/free try this. \$\endgroup\$
    – 11mad11
    Dec 18 '15 at 20:17
  • \$\begingroup\$ Ok, that seems to be the answer. Could you turn all of this into a answer? So I can mark it? :P \$\endgroup\$
    – BiiX
    Dec 18 '15 at 20:18
  • \$\begingroup\$ Another thing, according to a error NULL can only be used on int types. What if my type isn't an int? How should I define it without assigning memory to it? Just by not assigning something to it? \$\endgroup\$
    – BiiX
    Dec 18 '15 at 20:20
1
\$\begingroup\$

When allocating memory for an object in C++ using new, you must later deallocate it with delete or you will likely have a memory leak. In C, malloc corresponds with free.

When it comes to Box2D, world.CreateBody and world.DestroyBody handle b2Body creation and clean up for you. Additionally, you do not need to allocate the b2BodyDef, b2FixtureDef, and b2PolygonShape objects on the heap using new. Instead, create them on the stack like so:

b2BodyDef bodyDef;
bodyDef.position.Set(center.x + size/2, center.y + size/2);

b2Body* body = world.CreateBody(&bodyDef);

b2PolygonShape box;
box.SetAsBox(size / 2, size / 2.0f);

b2FixtureDef fixtureDef;
fixtureDef.shape = &box;
fixtureDef.friction = 0.3f;

body->CreateFixture(&fixtureDef);

Notice that no usage of new is necessary.

Finally, be careful when using SetUserData to store pointers to your own custom data in b2Body and b2Fixture using new. You must iterate over the body's fixtures and call delete on any user data that you stored in there.

You can learn more about the C++ stack vs. heap here: https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.