Is there any reason b2World.CreateBody() can silently fail?

I'm encountering a very strange bug in Box2d in which, after the application runs for a period of time that is generally between 15 - 30 minutes, b2World.CreateBody() will simply return null. It does this *in the middle of a for loop* in which nothing could conceivably set m_lock = true (I don't call b2World.Step() until much later after this is completed).

By this time, I've created and destroyed possibly thousands of bodies (all apparently successfully?). Is there some sort of counter somewhere that might be overflowing?
What version? There are some internal limits on the amounts you have at once, but those fail rather messily. There's nothing that should affect you after creating destroying many bodies. If you think it is a problem with the library, try to isolate the issue into a test case, built on the TestBed.
It's hard for me to know whether it's a library problem. What I do know is that I've never had more than ~350 bodies exist simultaneously. Usually it's more like ~280. A lot of them live only a short time, falling out of the world bounds and being destroyed after the Step() has completed, and if performance drops below ~26FPS I'm dynamically dropping the iterations (also *NOT* in the middle of a Step()) until it reaches 2 iterations at which point I destroy a bunch of them outright (again, only AFTER Step()).
I'm sure now that this must have been something I was doing wrong. I reverted my code to an older version and the error stopped happening.

Here's the finished product : http://www.scriptocalypse.com/ (it's the banner)

I just want to say you guys rock. This library is amazingly powerful and efficient.
And can I just say, nice blog posts. Informative and entertaining. I'm bookmarking your site for sure.
Thank you!

I'm sure I'll never run out of cautionary tales as long as I'm working with Flash. 😉 When to find the time to write all of them down, that's another story entirely! Hopefully I'll be able to keep up the pace of at least one per week.
Had the same error...a few hours later, I tracked down the problem and wanted to share a solution that worked for me in case others stumbled into same issue. (can't help but chuckle at prior comment that tried to blame the problem on Flash 😉 ) the problem (for me at least) is that the world has a boolean called m_lock..which when true causes createBody() to return null (just look at the code--i'm assuming m_lock occurs while world is stepping, solving stuff etcc.).... i kept getting this problem when I was switching between levels and creating bodies etc..

anyway, as far as I cantell, you just have to make sure that you don't try to create a body while world is stepping..sort of seemed obvious once I came to this conclusion! I was able to solve this by simply adding a single called to b2World.step() before re-creating the level and making that createBody call--just myB2World.Step(timeStep, 1) ... just wanted to share and possibly save others some hair pulling.
I'm removing silent failures of this sort for 2.1. You'll get a thrown exception instead, much easier to debug.
e96 wrote:Had the same error...a few hours later, I tracked down the problem and wanted to share a solution that worked for me in case others stumbled into same issue. (can't help but chuckle at prior comment that tried to blame the problem on Flash 😉 ) the problem (for me at least) is that the world has a boolean called m_lock..which when true causes createBody() to return null (just look at the code--i'm assuming m_lock occurs while world is stepping, solving stuff etcc.).... i kept getting this problem when I was switching between levels and creating bodies etc..

anyway, as far as I cantell, you just have to make sure that you don't try to create a body while world is stepping..sort of seemed obvious once I came to this conclusion! I was able to solve this by simply adding a single called to b2World.step() before re-creating the level and making that createBody call--just myB2World.Step(timeStep, 1) ... just wanted to share and possibly save others some hair pulling.


Thank you! Jesus I was stuck there 🙂)

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