C++ Max number of bodies

Hello people.
I'm new in Box2d and I don't know if I'm in the correct place to ask this, sorry.
I've the following problem: I'm working in a project where I have around more that 99 bodies. The problem is that when a do
b2Body** bodies = (b2Body**)b2Alloc(77 * sizeof(b2Body*)), it works ok but when I tried with more than 78 bodies, I've got a crash.
The same occurs if I use a large number of fixtures.
Could be something about the b2Alloc method?, using b2Body** bodies = new b2Body*[99] not worked
I'm using Box2d with marmalade.

Thanks
Does the crash in
Betiana wrote:
... I've got a crash.


... a crash when you call b2Alloc? Unless you have changed it, b2Alloc just calls malloc, and sizeof(b2Body*) is only 8 bytes at most, so even allocating 100 of them is only 800 bytes - I can't see why that would crash. Unless you are really, really low on memory 🙂

Or did you mean that the crash occurs somewhere else later?
Thanks for reply , sorry for not specifying 🙂

I'm working with Rube, on this I create the bodies and then export the source code in c++. The result is

Code:
b2Body** bodies = (b2Body**)b2Alloc(99 * sizeof(b2Body*));
b2Joint** joints = (b2Joint**)b2Alloc(0 * sizeof(b2Joint*));
{
  b2BodyDef bd;
  bd.type = b2BodyType(0);
  bd.position.Set(6.781416015625000e+002f, 1.536361789703369e+001f);
  bd.angle = 0.000000000000000e+000f;
  bd.linearVelocity.Set(0.000000000000000e+000f, 0.000000000000000e+000f);
  bd.angularVelocity = 0.000000000000000e+000f;
  bd.linearDamping = 0.000000000000000e+000f;
  bd.angularDamping = 0.000000000000000e+000f;
  bd.allowSleep = bool(4);
  bd.awake = bool(2);
  bd.fixedRotation = bool(0);
  bd.bullet = bool(0);
  bd.active = bool(32);
  bd.gravityScale = 1.000000000000000e+000f;
  bodies[0] = m_world->CreateBody(&bd);

  {
    b2FixtureDef fd;
    fd.friction = 2.000000029802322e-001f;
    fd.restitution = 0.000000000000000e+000f;
    fd.density = 1.000000000000000e+000f;
    fd.isSensor = bool(0);
    fd.filter.categoryBits = uint16(1);
    fd.filter.maskBits = uint16(65535);
    fd.filter.groupIndex = int16(0);
    b2PolygonShape shape;
    b2Vec2 vs[8];
    vs[0].Set(7.905740737915039e-001f, -7.905740737915039e-001f);
    vs[1].Set(7.905740737915039e-001f, 7.905745506286621e-001f);
    vs[2].Set(-7.905745506286621e-001f, 7.905745506286621e-001f);
    vs[3].Set(-7.905745506286621e-001f, -7.905740737915039e-001f);
    shape.Set(vs, 4);

    fd.shape = &shape;

    bodies[0]->CreateFixture(&fd);
  }
}


I put just one to show but I've 99 bodies.

When I load it in my project, I use a method that returns this array of bodies. Then, when I call this method, my application crashes.

I attach the first message that appears.
Unless you have changed it, that output will have this at the end:
Code:
b2Free(joints);
b2Free(bodies);

If you want to use the array later, you would need to remove that. This would not really explain why it works until 78 bodies, but strange things can happen when accessing freed memory.

I really recommend using the JSON loading instead of the c++ export, if you're doing anything more than a quick prototype. The c++ export method has no way to get a reference to bodies/fixtures etc by name, and it must be a pain in the butt to re-compile your program every time you edit the scene, and now it looks like you might have to edit the cpp file after every export as well 🙂
Yes, I use it and the of the method.

Honestly it is something that is driving me crazy. I've been struggling with this for several days 🙁

At the first time I used json but began to worry the loading time 'cause I have a level with more than 200 bodies and will continue to increase.

The message about the error that appears is "windows has triggered a break point in s3e_simulator_debug.exe.This may be due to corruption of the heap, which indicates a bug in javaw.exe or any of the DLLs it has loaded-. This may also be due to user pressing F12 while javaw.exe has focus. The output window may have more diagnostic information."

Searching and searching, I found this:
"The bucket-sizing factor must be a multiple of 8 for 32-bit implementations and a multiple of 16 for 64-bit implementations in order to guarantee that addresses returned from malloc subsystem functions are properly aligned for all data types. IBM.com"

It is really strange

So, I think I'll have to see how to reduce the loading time of a json file because honestly I do not know what else to do to fix this problem

Thank you very much for answering 🙂
I would not expect the loading time of JSON to be a significant issue... did you actually test it, or are you prematurely optimizing?
Take a look at the "Domino tower" example here: http://www.iforce2d.net/rube/loaders/javascript/
This scene has 1000 bodies, but even in Javascript it loads in a little over 1 second on my desktop PC (core-i5 3ghz + chrome browser), and that is including the network transfer time since the data loads over ajax. Loading that scene into the RUBE editor takes a little longer at almost 2 seconds, because it is setting up a lot of data structures behind the scenes to allow for features like mouse picking, scripting etc. I really don't think loading the scene into your game would take any longer than that.

If you are targeting mobile platforms of course the processing power is much more limited and things will be slower. In that case I think you'll find that 200 bodies is already pushing the limits of what most devices can do comfortably, especially if you want to pile them up on each other, so you will have other problems before the JSON loading speed becomes a priority.

Overall though, you should also be thinking about how much pain you will go through in the development process if you go with the c++ method 🙂 How will you get a reference to the bodies that you need to control in your game? And do you really want to re-compile your program every time you make a small tweak to your levels? If you use the JSON loader, you can just hit Shift+Space in RUBE to re-export the scene, and then restart the level in your game... takes a few seconds.

Original thread: https://box2d.org/forum/viewtopic.php?t=9739