why v8 don't call weak function

101 views
Skip to first unread message

Tom Fan

unread,
Dec 23, 2014, 4:53:45 AM12/23/14
to v8-u...@googlegroups.com
The NewType::~NewType is not called, the test code as follow.

#include "v8.h"                                                                                                           
#include "libplatform/libplatform.h"                                                                                      
                                                                                                                          
class NewType                                                                                                             
{                                                                                                                         
    public:                                                                                                               
        static void WeakPtrCallback(const v8::WeakCallbackData<v8::Object, NewType>& data)                                
        {                                                                                                                 
            delete data.GetParameter();                                                                                   
        }                                                                                                                 
        NewType(v8::Isolate *isolate, v8::Handle<v8::Object> handle): isolate_(isolate)                                   
        {                                                                                                                 
            printf("%s\n", __PRETTY_FUNCTION__);                                                                          
            handle_.Reset(isolate, handle);                                                                               
            handle_.SetWeak(this, WeakPtrCallback);                                                                       
        }                                                                                                                 
        ~NewType()                                                                                                        
        {                                                                                                                 
            handle_.Reset();                                                                                              
            printf("%s\n", __PRETTY_FUNCTION__);                                                                          
        }                                                                                                                 
    private:                                                                                                              
        v8::Persistent<v8::Object> handle_;                                                                               
        v8::Isolate *isolate_;                                                                                            
                                                                                                                          
};                                                                                                                        
                                                                                                                          
int main(int argc, char *argv[])                                                                                          
{                                                                                                                         
    v8::Platform* platform = v8::platform::CreateDefaultPlatform();                                                       
    v8::V8::InitializePlatform(platform);                                                                                 
    v8::V8::Initialize();                                                                                                 
    v8::V8::SetFlagsFromCommandLine(&argc, argv, true);                                                                   
    v8::Isolate* isolate = v8::Isolate::New();                                                                            
    {                                                                                                                     
        v8::Isolate::Scope isolate_scope(isolate);                                                                        
        v8::HandleScope handle_scope(isolate);                                                                            
        v8::Local<v8::Context> context = v8::Context::New(isolate);                                                       
        v8::Context::Scope context_scope(context);                                                                        
        v8::Local<v8::ObjectTemplate> tpl = v8::ObjectTemplate::New(isolate);                                             
        tpl->SetInternalFieldCount(1);                                                                                    
        v8::Local<v8::Object> value = tpl->NewInstance();                                                                 
        new NewType(isolate, value);                                                                                      
        context->Global()->Set(v8::String::NewFromUtf8(isolate, "test"), value);                                          
    }                                                                                                                     
    isolate->RequestGarbageCollectionForTesting(v8::Isolate::kFullGarbageCollection);                                     
    isolate->Dispose();                                                                                                   
    v8::V8::Dispose();                                                                                                    
    v8::V8::ShutdownPlatform();                                                                                           
    delete platform;                                                                                                      
}                                                                                                                         

Andreas Rossberg

unread,
Jan 7, 2015, 9:09:54 AM1/7/15
to v8-u...@googlegroups.com
From v8/include.h:

  /**
   *  Install a finalization callback on this object.
   *  NOTE: There is no guarantee as to *when* or even *if* the callback is
   *  invoked. The invocation is performed solely on a best effort basis.
   *  As always, GC-based finalization should *not* be relied upon for any
   *  critical form of resource management!
   */
  template<typename P>
  V8_INLINE void SetWeak(
      P* parameter,
      typename WeakCallbackData<T, P>::Callback callback);




--
--
v8-users mailing list
v8-u...@googlegroups.com
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 v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

啸宇 樊

unread,
Jan 7, 2015, 10:17:39 AM1/7/15
to v8-u...@googlegroups.com
Thanks, I have seen it.

You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/b-Xr_nZTFFI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages