V8中HandleScope周边栈损坏问题求助
运行V8嵌入代码时触发栈损坏错误:Run-Time Check Failure #2 - Stack around the variable 'handle_scope' was corrupted
我在运行以下嵌入V8引擎的C++代码时,遇到了栈损坏错误:Run-Time Check Failure #2 - Stack around the variable 'handle_scope' was corrupted。
代码实现
#include <iostream> #define V8_COMPRESS_POINTERS 1 #include <V8/v8.h> #include <V8/libplatform/libplatform.h> namespace consoleUtils { static void log(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::Isolate* isolate = args.GetIsolate(); if (args.Length() > 0) { v8::String::Utf8Value utf8(isolate, args[0]); std::cout << *utf8 << std::endl; } } } class Console { v8::Persistent<v8::FunctionTemplate> logTemplate; public: Console(v8::Isolate* isolate, v8::Local<v8::Context> context) { v8::Local<v8::FunctionTemplate> logFuncTemplate = v8::FunctionTemplate::New(isolate, consoleUtils::log); logTemplate.Reset(isolate, logFuncTemplate); context->Global()->Set(context, v8::String::NewFromUtf8(isolate, "log").ToLocalChecked(), logFuncTemplate->GetFunction(context).ToLocalChecked()).Check(); } ~Console() { logTemplate.Reset(); } }; class Engine { private: v8::Isolate::CreateParams create_params; v8::Persistent<v8::Context> context; std::unique_ptr<v8::Platform> platform; public: v8::Isolate* isolate = nullptr; Console* console = nullptr; Engine() { // Initialize V8. v8::V8::InitializeICUDefaultLocation("./v8-data"); v8::V8::InitializeExternalStartupData("./v8-data"); platform = v8::platform::NewDefaultPlatform(); v8::V8::InitializePlatform(platform.get()); v8::V8::Initialize(); // Create a new Isolate and make it the current one. create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); isolate = v8::Isolate::New(create_params); // Create a new context. v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Local<v8::Context> local_context = v8::Context::New(isolate); context.Reset(isolate, local_context); // Initialize console with the current context. console = new Console(isolate, local_context); } ~Engine() { delete console; context.Reset(); isolate->Dispose(); v8::V8::Dispose(); delete create_params.array_buffer_allocator; } void runExample() { v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Local<v8::Context> local_context = context.Get(isolate); v8::Context::Scope context_scope(local_context); v8::Local<v8::String> source = v8::String::NewFromUtf8Literal(isolate, "log('Hello, World!')"); v8::Local<v8::Script> script = v8::Script::Compile(local_context, source).ToLocalChecked(); v8::Local<v8::Value> result = script->Run(local_context).ToLocalChecked(); v8::String::Utf8Value utf8(isolate, result); std::cout << *utf8 << std::endl; } }; int main(int argc, char* argv[]) { Engine* engine = new Engine(); engine->runExample(); delete engine; return 0; }
V8构建配置(args.gn)
is_debug = true target_cpu = "x64" v8_enable_backtrace = true v8_enable_slow_dchecks = true v8_optimized_debug = false is_component_build = false v8_static_library = true v8_monolithic = true v8_use_external_startup_data = false use_custom_libcxx = false is_clang = false # Disable WebAssembly v8_enable_webassembly = false v8_enable_wasm_simd256 = false v8_enable_pointer_compression = true v8_enable_sandbox = false enable_iterator_debugging = true
环境信息
- IDE:Microsoft Visual Studio
- 编译器:MSVC
- C标准:C20
- 已尝试的操作:自行调试未取得进展,重新安装Visual Studio并重新构建V8,问题仍存在
- 额外说明:使用指针管理Engine类是因为类体积较大,避免栈溢出
内容的提问来源于stack exchange,提问作者BLAD231353
相关产品推荐
相关产品推荐

