gpt4 book ai didi

c++ - 如何在 NodeJs native 插件中使用非包装 C++ 对象的 std::map

转载 作者:太空宇宙 更新时间:2023-11-04 13:40:24 25 4
gpt4 key购买 nike

我真的不明白为什么我的插件会随机崩溃:

Fatal error in ../deps/v8/src/api.h, line 297
CHECK(allow_empty_handle || that != __null) failed

目前我认为这可能是我的代码中与内存相关的问题,但我不确定我的方法是否正确。

这里是一些代码细节:

我的 C++ 插件仅在主 NodeJs 应用程序启动时加载一次。主应用程序可以调用我的插件中的几个导出函数。其中一个函数(在下面命名为“Initialize”)获得一个唯一的 id,用于从全局定义的 std::map 对象(在下面命名为 g_mapObject)中查找 C++ 对象(在下面命名为“MyObject”)实例.如果在 map 中找不到具有给定键的对象,则“初始化”函数会创建一个对象并将其插入到 map 中,该对象的新实例然后初始化该对象。其他导出函数会定期从主应用程序调用。他们从 map 中检索对象并对其进行操作。

在我的 MyAddon.cc 中:

#include "MyAddon.h"
#include "MyObject.h"

using namespace v8;

std::map<string, MyObject*> g_mapObject;

Handle<Value> MyAddon::Initialize(const Arguments& args) {
HandleScope scope;

String::AsciiValue id(args[0]->ToString());

// Get the object from g_mapObject and initialize it.
// If the object is not found then create and insert it into map.


return scope.Close(0);
}

Handle<Value> MyAddon:UpdateObjectData1(const Arguments& args) {
HandleScope scope;

String::AsciiValue id(args[0]->ToString());

// Get the object from g_mapObject and update its data

return scope.Close(0);
}

Handle<Value> MyAddon::UpdateObjectData1(const Arguments& args) {
HandleScope scope;

String::AsciiValue id(args[0]->ToString());

// Get the object from g_mapObject and update its data

return scope.Close(0);
}

每个方法都用 uv_mutex_lock/unlock 调用包装,因此确保和测试方法调用是同步的。 fatal error 发生在“初始化”方法中(但从未在其第一次调用时发生)。

PS:顺便提一下,我使用的是 C++11。

最佳答案

我的做法是对的,但是在实现上存在逻辑错误。

关于c++ - 如何在 NodeJs native 插件中使用非包装 C++ 对象的 std::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27881176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com