Executing a script will occur an error "Access violation executing location"

104 views
Skip to first unread message

Kazuya Hiruma

unread,
May 5, 2022, 8:41:57 AM5/5/22
to v8-users
I'm new to C++ and V8.

I'm googling some scripts for creating a V8 engine wrapper.

I refered some implementations to implement my system. I'm so confused why the error is occurred.

My all code are below.

1. Header

#pragma once
#include <v8.h>

namespace v8_api
{
    class Core
    {
    private:
        v8::Isolate* isolate_;
        v8::Global<v8::Context> impl_;

    public:
        void Initialize();
        void Run(const char* source_code);
    };
}



2. Implementation

#include <iostream>
#include "include/v8.h"
#include "include/libplatform/libplatform.h"
#include "v8api.h"

namespace v8_api
{
    void Core::Initialize()
    {
        std::cout << "Initializing v8_api::Core\n";

        v8::V8::InitializeICU();

        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();
        isolate_ = v8::Isolate::New(create_params);

        v8::Isolate::Scope isolate_scope(isolate_);
        v8::HandleScope scope(isolate_);
        v8::Local<v8::Context> impl = v8::Context::New(isolate_);
        impl_.Reset(isolate_, impl);
    }

    void Core::Run(const char* source_code)
    {
        v8::HandleScope handle_scope(isolate_);
        {
            v8::Local<v8::Context> context = v8::Context::New(isolate_);
            v8::Context::Scope context_scope(context);
            {
                v8::MaybeLocal<v8::String> m_source = v8::String::NewFromUtf8(isolate_, source_code);
                v8::Local<v8::String> source = m_source.ToLocalChecked();
                v8::MaybeLocal<v8::Script> m_script = v8::Script::Compile(context, source);
                v8::Local<v8::Script> script;
                if (!v8::Script::Compile(context, source).ToLocal(&script))
                {
                    return;
                }
                v8::Local<v8::Value> result = script->Run(isolate_->GetCurrentContext()).ToLocalChecked();
                v8::String::Utf8Value resultStr(isolate_, result);
                std::cout << *resultStr;
            }
        }
    }
}



3. A code that runs the system.

#include <iostream>
#include <thread>
#include "v8.h"
#include "v8api.h"

int main(int argc, char* argv[])
{
    std::cout << "Hello World!\n";

    v8_api::Core* core = new v8_api::Core();
    core->Initialize();

    const char* src = "var a = 4;";
    core->Run(src);

    std::this_thread::sleep_for(std::chrono::milliseconds(1000));

    return 0;
}



What my question is that why compiling will fail.

The steps are below. (This is done by third code)

1. Create an instance of "Core".
2. Initialize the Core.
3. Invoke its method "Run" with a JavaScript code.
4. It happens the error "Access violation executing location" !

What the error is below.
error-screenshot.png

I noticed that "m_script" is NULL but I'm not sure why it returns NULL. The JavaScript code is so simple and correct I think.

--------------------------------

Additional Info:
If I run a JavaScript code in the "Initialize" method like below. After that, all executing "Run" method work fine with no error.

v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope scope(isolate_);
v8::Local<v8::Context> impl = v8::Context::New(isolate_);
impl_.Reset(isolate_, impl);

const char* src = "var a = 30.5;";
Run(src);


After that, the code below will work fine. (This is in third code)

const char* src = "var a = 4;";
core->Run(src);


I'm sooo confused. Is there any my mistake?

Kazuya Hiruma

unread,
May 5, 2022, 7:24:11 PM5/5/22
to v8-users
I solved this problem. The reason is I decleared `std::unique_ptr<v8::Platform>` as local variable then its destructor will be invoked at last of the `Initialize` method.

I changed to store the value on the `Core` class side then the problem was solved.

2022年5月5日木曜日 21:41:57 UTC+9 Kazuya Hiruma:
Reply all
Reply to author
Forward
0 new messages