kripken / box2d.js

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

b2Vec2 won't free #100

Closed SneakyFernando closed 6 years ago

SneakyFernando commented 6 years ago

I am trying to figure out how to free vectors.

var vector = new Box2D.b2Vec2(x, y);    

var ptr1 = Box2D._malloc(0);
Box2D._free(ptr1);
Box2D._free(vector);
var ptr2 = Box2D._malloc(0);
Box2D._free(ptr2);

Currently when I run this, I would get memory leak eventually. Debugging shows that ptr1 and ptr2 are equal. I expect ptr2 to have -8 offset. What am I doing wrong?

kripken commented 6 years ago

You need to free what you malloc, but when you call new Box2D.b2Vec2 it calls a c++ constructor, which you need to run the right destructor for. See here, the syntax is Ammo.destroy(object).

SneakyFernando commented 6 years ago

@kripken, thanks for the accurate answer and provided link!