Memory leak

Hi!
I've encountered a problem with memory leak in my program
And i checked if this is my fault
The following code allocates about 130 mb
(i saw it in task menager)

b2BodyDef newbody;
for(int i = 0; i < 1000000; i++)
{
gWorld->DestroyBody(gWorld->CreateBody(&newbody));
}

Could you please help me what is wrong with this?

Thank you
Have you tried calling b2World->Step() ? Box2D probably leaves some memory behind until the world is updated 😉
Yes, Box2D defers body destruction so that you may destroy bodies while processing contacts. This happens a lot in games, especially with breakable objects.

Because the body destruction is deferred, the world accumulates 1000000 bodies. The memory pool makes space for all these bodies and the pool is designed to keep the memory until the world is destroyed. This makes memory alloc and free very fast.

Do you have practical use case where this design is a problem?
Erin Catto wrote:
Do you have practical use case where this design is a problem?


No,
I'm writting a game where I have to create and destroy 1-2 bodies per second. This allocates 8kb per minute
so it isn't very horrible (in additional world is refreshed every new level) and 16mb would fit for 36 hours of levelplay..
Ok then, I understood. Thanks
The memory usage should grow according to the peak usage. After reaching the peak usage, the memory allocation should not grow.

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