I used cocos2d-x using C++ to develop my game since I’ve already created “Entity-Component System” framework and the core gameplay for the game. Then I’ve decided to add more flexibility by using scripts for some specific mechanic and all the UI view of the game, so I choose JS over lua but after a while I found out that all the deleted objects on the C++ side is not removed on the JS side. It looks like the proxy objects are still there and not removed because I didn’t call garbage collect anywhere. I don’t know how to do memory management on JS, can anyone help me with it? should I call the garbage collector manually on C++ every x sec(s) or should I put it on the constructor/onEnter callback of every JS class I’ve created like on the js-tests
You can’t force JavaScript garbage collection. The best you can do is set values to null and hope the GC takes the hint and picks up on it (timing isn’t predictable).
so javascript garbage collection is handled automatically every x time? then I have to set all the proxy objects to null and that will be difficult. Okay I’ll try to do that, thanks for your help. But why on js-tests project, I saw that BaseTestLayer class’ constructor call garbageCollect and the class will be inherited to every layer
Interesting. I’m brand new to js in cocos2dx, used to plain web js.
Looks like you’re right, it can be forced. Gonna look more into this.
They bound js to a c++ function with the signature
static bool forceGC(JSContext *cx, uint32_t argc, jsval *vp); in ScriptingCore (/cocos/scripting/js-bindings/manual/ScriptingCore.h).
Maybe you can call that from the C++ side (using the ScriptingCore singleton) to let the js side know it should GC?