此代码在Windows / Visual Studio上运行正常,但在Ubuntu / GCC上进行分段。为什么呢?
我已将V8 4.5.8编译为共享库。我已将以下代码放入CPPUNIT测试中。
/**
* A simple test for basic V8 functionality, just running the "Hello World" example from
* from <a href="https://developers.google.com/v8/get_started">the getting started guide</a>.
**/
CPPUNIT_QUICKTEST(V8Basics, GettingStartedGuide)
{
v8::V8::InitializeICU();
v8::Platform * platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
// Create a new Isolate and make it the current one.
Isolate* isolate = Isolate::New();
{
Isolate::Scope isolate_scope(isolate);
// Create a stack-allocated handle scope.
HandleScope handle_scope(isolate);
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
// Compile the source code.
Local<Script> script = Script::Compile(source);
// Run the script to get the result.
Local<Value> result = script->Run();
// Convert the result to an UTF8 string and print it.
String::Utf8Value utf8(result);
CPPUNIT_ASSERT_STRING_EQUAL("Hello, World!", std::string(*utf8));
}
// Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete platform;
}
此测试使用以下堆栈进行段错误。这个堆栈的大部分是CppUnit和封闭的测试可执行文件。
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00007f75cabf8ce9 in v8::internal::Genesis::InitializeBuiltinTypedArrays() ()
from /my-working-dir/Debug/bin/../lib/libv8.so
(gdb) bt
#0 0x00007f75cabf8ce9 in v8::internal::Genesis::InitializeBuiltinTypedArrays() ()
from /my-working-dir/Debug/bin/../lib/libv8.so
#1 0x00007f75cabfe743 in v8::internal::Genesis::Genesis(v8::internal::Isolate*, v8::internal::MaybeHandle<v8::internal::JSGlobalProxy>, v8::Local<v8::ObjectTemplate>, v8::ExtensionConfiguration*) () from /my-working-dir/Debug/bin/../lib/libv8.so
#2 0x00007f75cabed179 in v8::internal::Bootstrapper::CreateEnvironment(v8::internal::MaybeHandle<v8::internal::JSGlobalProxy>, v8::Local<v8::ObjectTemplate>, v8::ExtensionConfiguration*) () from /my-working-dir/Debug/bin/../lib/libv8.so
#3 0x00007f75cabb4e50 in v8::Context::New(v8::Isolate*, v8::ExtensionConfiguration*, v8::Local<v8::ObjectTemplate>, v8::Local<v8::Value>) ()
from /my-working-dir/Debug/bin/../lib/libv8.so
#4 0x00007f75ccc34fd0 in V8Basics_GettingStartedGuide::TestBody (this=0x25fda50) at v8core_v8basics_tests.cpp:31
#5 0x00007f75ccc374e2 in CppUnit::TestCaller<V8Basics_GettingStartedGuide>::runTest (this=0x2600a80)
at ../../libs/3rdParty/include/cppunit/TestCaller.h:166
#6 0x00007f75cc2b8012 in CppUnit::TestCaseMethodFunctor::operator() (this=<optimised out>) at TestCase.cpp:32
#7 0x00007f75cd4d6793 in testsuite::ExceptionProtector::protect (this=0x25fef10, functor=..., context=...) at testsuite_exceptionprotector.cpp:27
#8 0x00007f75cc2b535d in CppUnit::ProtectorChain::protect (this=0x25ffef0, functor=..., context=...) at ProtectorChain.cpp:77
#9 0x00007f75cc2bdb7a in CppUnit::TestResult::protect (this=this@entry=0x7ffc69f1ace0, functor=..., test=test@entry=0x2600a80, shortDescription=...)
at TestResult.cpp:178
#10 0x00007f75cc2b7d1a in CppUnit::TestCase::run (this=0x2600a80, result=0x7ffc69f1ace0) at TestCase.cpp:92
#11 0x00007f75cc2b8363 in CppUnit::TestComposite::doRunChildTests (this=0x2600600, controller=0x7ffc69f1ace0) at TestComposite.cpp:64
#12 0x00007f75cc2b827e in CppUnit::TestComposite::run (this=0x2600600, result=0x7ffc69f1ace0) at TestComposite.cpp:23
#13 0x00007f75cc2b8363 in CppUnit::TestComposite::doRunChildTests (this=0x26005a0, controller=0x7ffc69f1ace0) at TestComposite.cpp:64
#14 0x00007f75cc2b827e in CppUnit::TestComposite::run (this=0x26005a0, result=0x7ffc69f1ace0) at TestComposite.cpp:23
#15 0x00007f75cd4d9739 in testsuite::TestRunner::Impl::ExecuteTests (this=0x25c3a50, suites=..., registry=0x26005a0, controller=..., result=...)
at testsuite_testrunner.cpp:217
#16 0x00007f75cd4d9158 in testsuite::TestRunner::Run (this=0x7ffc69f1af08) at testsuite_testrunner.cpp:187
#17 0x0000000000426ad0 in testsuite::TestSuiteApplication::ExecuteTests (this=0x7ffc69f1aef0) at testsuite_testsuiteapplication.cpp:308
#18 0x0000000000426a2b in testsuite::TestSuiteApplication::Run (this=0x7ffc69f1aef0) at testsuite_testsuiteapplication.cpp:288
#19 0x0000000000422741 in main (argc=3, argv=0x7ffc69f1b018, env=0x7ffc69f1b038) at testsuite_main.cpp:64
为什么我会出现故障,我该如何解决?
我成功的Windows版本使用V8 4.5.8,Windows 8.1,Visual Studio 2013。 我不成功的Ubuntu版本使用GCC 4.8.2,Ubuntu 14.04.2。
答案 0 :(得分:3)
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
孤立:
// Create a new Isolate and make it the current one.
ArrayBufferAllocator allocator;
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
Isolate* isolate = Isolate::New(create_params);