C++ v8嵌入程序创建v8::Context是否有限制?
在MSVC2015发布模式下,当for循环在680~685之间运行时,我的程序总是崩溃。 以下是我的密码:C++ v8嵌入程序创建v8::Context是否有限制?,c++,v8,embedded-v8,C++,V8,Embedded V8,在MSVC2015发布模式下,当for循环在680~685之间运行时,我的程序总是崩溃。 以下是我的密码: #include <stdio.h> #define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <string.h> #include "libplatform/libplatform.h" #include "v8.h" #include <iostream> #include
#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include "libplatform/libplatform.h"
#include "v8.h"
#include <iostream>
#include <string>
int main(int argc , char* argv[] )
{
v8::V8::InitializeICUDefaultLocation(".");
v8::V8::InitializeExternalStartupData(".");
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope scope(isolate);
for (int i = 0; i < 1000; ++i) {
printf("loop %d\n", i);
v8::Local<v8::Context> tempContext = v8::Context::New(isolate);//
v8::Context::Scope context_scope(tempContext);
v8::TryCatch try_catch(isolate);
std::string debugScript = "let b=1;";
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, debugScript.c_str(),
v8::NewStringType::kNormal).ToLocalChecked();
v8::Local<v8::Script> script = v8::Script::Compile(tempContext, source).ToLocalChecked();
script->Run(tempContext).ToLocalChecked();// crash in loop 680~685
}
return 0;
}
#包括
#定义\u CRT\u安全\u无\u警告
#包括
#包括
#包括“libplatform/libplatform.h”
#包括“v8.h”
#包括
#包括
int main(int argc,char*argv[])
{
v8::v8::InitializeCudeFaultLocation(“.”);
v8::v8::InitializeExternalStartUpdatea(“.”);
std::unique_ptr platform=v8::platform::NewDefaultPlatform();
v8::v8::InitializePlatform(platform.get());
v8::v8::Initialize();
v8::Isolate::CreateParams创建_参数;
创建参数数组缓冲区分配器=
v8::ArrayBuffer::分配器::NewDefaultAllocator();
v8::Isolate*Isolate=v8::Isolate::New(创建参数);
v8::隔离::范围隔离\范围(隔离);
v8::手镜范围(隔离);
对于(int i=0;i<1000;++i){
printf(“循环%d\n”,i);
v8::Local tempContext=v8::Context::New(隔离)//
v8::Context::Scope Context_Scope(tempContext);
v8::TryCatch try_catch(隔离);
std::string debugScript=“let b=1;”;
v8::Local source=v8::String::NewFromUtf8(隔离,debugScript.c_str(),
v8::NewStringType::kNormal).ToLocalChecked();
v8::Local script=v8::script::Compile(tempContext,source).ToLocalChecked();
脚本->运行(tempContext).ToLocalChecked();//在循环680~685中崩溃
}
返回0;
}
为什么代码总是在同一时间崩溃?崩溃的原因是什么?谢谢@jmrk,同样的代码在ubuntu中运行时不会崩溃。我不知道为什么。谢谢@jmrk。我使用来自nuget(v8-7.4.288.11)的预构建二进制文件,并且没有pdb文件,因此没有stacktrace。我从源代码在Ubuntu中构建了v8(8.2.0.0),同样的代码在Ubuntu中运行时不会崩溃。我不知道为什么。那么,试试用MSVC在调试模式下编译吧。无论哪种方式,要调试崩溃,您(或任何试图帮助您的人)都需要堆栈跟踪。您应该尝试在循环中移动句柄范围。