kripken / box2d.js

Port of Box2D to JavaScript using Emscripten
1.32k stars 196 forks source link

Memory leak #90

Open timmyL17 opened 7 years ago

timmyL17 commented 7 years ago

Hi,

I'm seeing an issue where I think world.DestroyBody does not completely clean everything up. I've created a jsfiddle here showing it: https://jsfiddle.net/otx84Lfb/12/

There is a debugging function in there that figures out how much memory is left on the heap. When creating b2BodyDef objects and destroying them, the available memory is exactly the same. After creating and destroying b2Body objects using the world.DestroyBody function, some memory is lost. In our application, this causes the emscripten heap to eventually fill up.

Is there something else I'm supposed to do when destroying a body? Thanks.

timmyL17 commented 7 years ago

Two updates: 1) This also happens with version 2.3.1 (https://jsfiddle.net/otx84Lfb/13).

2) It appears to be due to the small block allocator. If I recompile after changing Allocate in b2BlockAllocator.cpp to just return b2Alloc(size), and Free in b2BlockAllocator.cpp to just return b2Free(p), then the memory leak goes away.

calvinte commented 6 years ago

@timmyL17 perhaps you should report this to https://github.com/erincatto/Box2D.

I believe you are referencing this line: https://github.com/erincatto/Box2D/blob/master/Box2D/Box2D/Common/b2BlockAllocator.cpp#L106

soylomass commented 4 years ago

Was this fixed?

joex92 commented 3 years ago

I'm having the same issue... is there an update?

Birch-san commented 3 years ago

Tried this with box2d-wasm (branch: memory-leak-repro) against Box2D v2.4.1 sources, and I encounter the same memory leak:

mem start:
11411808

mem after body def destroy:
11411808

mem after destroy bodies__:
11395384

count:
0

items:
0

The library distribution against which I tested is here (only thing different compared to the npm release is I configured ALLOW_MEMORY_GROWTH=0 in order for the checkMemory function in this repro to work correctly): box2d-wasm-1.0.2.tar.gz