我的代码:
struct WeakData {
context* c;
std::vector<UniquePersistent<Object>>::iterator i;
};
void WeakPtrCallback(const v8::WeakCallbackData<v8::Object, WeakData>& data) {
printf("weak callback");
fflush(stdout);
// crashes always after exit from this callback
}
Local<Object> wrapDocumentSelection(Isolate* isolate, cDocumentSelection* selection) {
EscapableHandleScope handle_scope(isolate);
auto ctx = isolate->GetCurrentContext();
Context::Scope context_scope(ctx);
Local<External> contextWrap = Local<External>::Cast(
Handle<Object>::Cast(isolate->GetCurrentContext()->Global()->GetPrototype())->GetInternalField(0));
context* c = static_cast<context*>(contextWrap->Value());
Local<ObjectTemplate> documentSelectionTemplate = Local<ObjectTemplate>::New(isolate, c->documentSelectionTemplate);
Local<Object> jsSelection = documentSelectionTemplate->NewInstance(ctx).ToLocalChecked();
jsSelection->SetInternalField(0, External::New(isolate, selection));
jsSelection->Set(ctx, String::NewFromUtf8(isolate, "length"), Integer::New(isolate, selection->Length));
c->trackingObjects.push_back(UniquePersistent<Object>(isolate, jsSelection));
auto &p = c->trackingObjects.back();
auto it = c->trackingObjects.end();
it--;
WeakData* wd = new WeakData;
wd->c = c;
wd->i = it;
p.Reset(isolate, jsSelection);
p.SetWeak<WeakData>(NULL, WeakPtrCallback);
isolate->AdjustAmountOfExternalAllocatedMemory(1024);
return handle_scope.Escape(jsSelection);
}
在上下文中我保持所有持久句柄的向量 在WeakPtrCallback中,我需要清理一些外部分配的资源
v8错误:
#
# Fatal error in ../src/global-handles.cc, line 325
# Check failed: state() != NEAR_DEATH.
#
关于https://strongloop.com/strongblog/node-js-v0-12-c-apis-breaking/ 我不需要在回调中执行任何与v8相关的清理
自动处理持久性存储单元。如果你想用.ClearWeak()恢复它,请保留对原始v8 :: Persistent的引用。
我也试过使用Persistent和Persistent<Value, CopyablePersistentTraits<Value>>
- 同样的错误......
有什么建议吗?
答案 0 :(得分:0)
好吧,我的C ++似乎不够好 我跟踪问题以解决向量问题,由于某种原因它无法正确删除所需的元素,我猜迭代器是索引绑定的,所以删除一些先前的元素会破坏所有进一步的迭代器引用......
切换到std :: map并且不断递增计数器作为键完全解决了问题