#include <stdlib.h>
#include <stdio.h>
#include <v8.h>
#include <libplatform/libplatform.h>
using namespace v8;
class MallocAllocator : public ArrayBuffer::Allocator {
public:
virtual void *Allocate(size_t size) {
void *data = AllocateUninitialized(size);
if (data != NULL)
memset(data, 0, size);
return data;
}
virtual void *AllocateUninitialized(size_t size) {
return malloc(size);
}
virtual void Free(void *data, size_t size) {
free(data);
}
};
void func_callback(const FunctionCallbackInfo<Value> &info) {}
void weak_callback(const WeakCallbackInfo<Persistent<String>> &info) {
Persistent<String> *handle = info.GetParameter();
printf("weak callback called with %p\n", handle);
delete handle;
}
int main() {
V8::InitializeICU();
Platform *current_platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(current_platform);
V8::Initialize();
V8::SetFlagsFromString("--expose_gc", strlen("--expose_gc"));
Isolate::CreateParams params;
params.array_buffer_allocator = new MallocAllocator();
Isolate *isolate = Isolate::New(params);
HandleScope hs(isolate);
Local<Context> context = Context::New(isolate);
Local<String> key = String::NewFromUtf8(isolate, "test", NewStringType::kNormal).ToLocalChecked();
Persistent<String> *weak_str = new Persistent<String>();
{
HandleScope hs(isolate);
Local<String> str = String::NewFromUtf8(isolate, "hi there", NewStringType::kNormal).ToLocalChecked();
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate, func_callback, str);
weak_str->Reset(isolate, str);
weak_str->SetWeak(weak_str, weak_callback, WeakCallbackType::kParameter);
context->Global()->Set(context,
key, templ->GetFunction(context).ToLocalChecked())
.FromJust();
}
context->Global()->Delete(context, key).FromJust();
isolate->RequestGarbageCollectionForTesting(Isolate::GarbageCollectionType::kFullGarbageCollection);
return 0;
}
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!
int64_t v8::Isolate::AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes)
In this program, weak_callback never gets called. Anyone know why?
--
--
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.
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
Interesting. So when a function template is created, its callback data will never be freed?~Theodore
On Oct 4, 2016, at 1:17 AM, Jochen Eisinger <joc...@chromium.org> wrote:
v8 has an per-isolate cache of function templates. As the string is referenced by the function template, it will never go out of scope.I'd recommend to create a weak handle to the function you create via GetFunction() instead
On Mon, Oct 3, 2016 at 12:27 PM Pavel Medvedev <pmed...@gmail.com> wrote:
Hi Theodore,In this program, weak_callback never gets called. Anyone know why?
There is no isolate->Enter() after Isolate::New(params), and no context->Enter() after Context::New(isolate). May it be a reason?
--
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
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.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+u...@googlegroups.com.
--
--
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.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
Put a(mother) handle scope inside the for loop
--
Good news is, this program calls the callback. Bad news it crashes, but I think it demonstrates the general idea.
I believe the problem with your program is that your global with the weak callback goes away too early. In my program below you can see I'm allocating the global on the heap (and leaking the crap out of it - but you can clean it up in the callback) and that makes the callback get called because the global is still around.
caveat emptor
P.S. Posting a compilable program next time makes it easier for me to help you
#include <v8.h>
#include <v8-platform.h>
#include <libplatform/libplatform.h>
#include <stdio.h>
using namespace v8;
void func_callback(const FunctionCallbackInfo<v8::Value> &data) {
fprintf(stderr, "Here\n");
}
void weak_callback(const WeakCallbackInfo<int> &data) {
fprintf(stderr, "weak callback\n");
}
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
inline virtual void* Allocate(size_t length) override {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
inline virtual void* AllocateUninitialized(size_t length) override { return malloc(length); }
inline virtual void Free(void* data, size_t) override { free(data); }
};
int main() {
V8::InitializeICU();
Platform *current_platform = v8::platform::CreateDefaultPlatform();
V8::InitializePlatform(current_platform);
V8::Initialize();
V8::SetFlagsFromString("--expose_gc", strlen("--expose_gc"));
Isolate::CreateParams params;
params.array_buffer_allocator = new ArrayBufferAllocator();
Isolate *isolate = Isolate::New(params);
HandleScope hs(isolate);
Local<Context> context = Context::New(isolate);
int iii; // bogus int
Local<ObjectTemplate> ot = ObjectTemplate::New(isolate);
ot->SetInternalFieldCount(1);
for (int i = 0; ; i++) {
HandleScope hs2(isolate);
Local<Object> obj = ot->NewInstance(context).ToLocalChecked();
Global<Object> * gobject = new Global<Object>(isolate, obj);
gobject->SetWeak<int>(&iii, weak_callback, WeakCallbackType::kParameter);
isolate->AdjustAmountOfExternalAllocatedMemory(100000000);
if (i%1000==0)printf("%d\r", i);
/* context->Global()->Set(context, key, func).FromJust(); */
/* context->Global()->Delete(context, key).FromJust(); */
}
return 0;
Actually, that didn't work. Any other ideas? Any techniques for debugging GC (like getting information on where an object is referenced?)
--
> To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> 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/Rshbi2HKYCM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
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/Rshbi2HKYCM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.