Hello, I'm working on a game engine that uses V8 JavaScript for scripting, and I'm exposing a Sprite class to the JavaScript API, and I've run into a problem: it isn't being garbage collected.
Here's how I'm wrapping the class:
v8::Persistent<v8::ObjectTemplate> _template;
// ...
v8::Handle<v8::ObjectTemplate> CreateTemplate() {
v8::HandleScope handle_scope;
v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New();
// ... set some methods ...
v8::Handle<v8::ObjectTemplate> t = templ->InstanceTemplate();
t->SetInternalFieldCount(1);
return handle_scope.Close(t);
}
FUNC_TYPE Wrap(FUNC_ARGS) {
v8::HandleScope handle_scope;
if (args.IsConstructCall()) {
v8::String::Utf8Value filename(args[0]);
Video::Sprite* s = new Video::Sprite(Scripting::ToCString(filename));
// Create the class template if it doesn't exist yet
if (_template.IsEmpty()) {
_template = v8::Persistent<v8::ObjectTemplate>::New(CreateTemplate());
}
v8::Persistent<v8::Object> result = v8::Persistent<v8::Object>::New(_template->NewInstance());
result.MakeWeak(s, WeakRefCallback);
result->SetInternalField(0, v8::External::New(s));
return handle_scope.Close(result);
}
return v8::Undefined();
}
void WeakRefCallback(v8::Persistent<v8::Value> object, void* parameter) {
Video::Sprite* s = static_cast<Video::Sprite*>(parameter);
delete s;
object.Dispose();
}
I'm testing the garbage collection by continually creating and deleting sprites in the JavaScript. However the WeakRefCallback is never called, and the memory usage continues to rise and rise. I've checked to make sure that the GC is running (with v8::V8::AddGCEpilogueCallback) and it is, quite frequently, however this is not disposing of the deleted JavaScript sprites.
Anyone have any ideas as to why?