On Thu, Oct 24, 2013 at 5:25 AM, Yorath Wang <[email protected]> wrote:
> I am using v8 (tag 3.22.19) built as shared library, the environment is
> windows 8.1 x64, visual studio 2010.
> I use External::new(ptr)  to pass the pointer to Print function, but the ptr
> is allocated use new, I don't know how to free the memory.
> Then i came across the API SetWeak, but there is no document at all. I try
> to use it with the codes below, but the WeakPtrCallback is never called,
> which leads to the memory leak.
> Anyone knows how to deal with this problem? Thanks!
>
> #include <v8.h>
> #include <vld.h>
> #include <memory>
>
> void Print(const v8::FunctionCallbackInfo<v8::Value>& data) {
>
> }
>
> void WeakPtrCallback(
>     const v8::WeakCallbackData<v8::Object, std::weak_ptr<int>>& data) {
>   delete data.GetParameter();
> }
>
> int main() {
>   auto isolate = v8::Isolate::GetCurrent();
>   v8::HandleScope handle_scope(isolate);
>   auto context = v8::Context::New(isolate);
>
>   auto ptr = std::shared_ptr<int>(new int(3));
>   auto data = new std::weak_ptr<int>(ptr);
>
>   auto global = context->Global();
>   auto func_tpl = v8::FunctionTemplate::New(Print, v8::External::New(data));
>
>   context->Enter();
>
>   auto func = func_tpl->GetFunction();
>   global->Set(v8::String::New("print"), func);
>   auto obj = v8::Persistent<v8::Object>(isolate, global);
>   obj.SetWeak<std::weak_ptr<int>>(data, WeakPtrCallback);
>
>   context->Exit();
>   obj.Reset();
>
>   return 0;
> }

Your callback runs at some undefined time in the future when the
garbage collector has determined that there are no more references to
the weak object.  If your program is short-lived, it may never run.

Of course, if you call v8::Persistent<T>::Reset(), it never runs
because you've zapped the persistent storage cell.

-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to