为什么这个指针指向任何地方?
我正在尝试向 v8sharp 项目添加功能,但遇到了一些问题(我不太擅长 C++-CLI,所以我很确定问题在于我缺乏 C++-CLI 能力,而不是滥用 v8 .)
v8value.cpp:
v8sharp::V8FunctionWrapper^ V8ValueWrapper::WrapFunction(v8::Handle<v8::Value> value) {
// Now we use the wrapper to make this safe to use
// this works
Console::WriteLine("IsFunction-First: {0}", value->IsFunction());
// persistent so it doesn't get garbage collected
v8::Persistent<v8::Value> pval(value);
// create a function wrapper
V8FunctionWrapper^ bla = gcnew V8FunctionWrapper(pval);
return bla;
}
它应该采用一个 v8Handle
,其中包含一个函数(它总是会因为调用该函数的原因)并返回一个漂亮的 .net 包装器,以便我们可以使用它在我的 C# 项目中。
问题就出在这里 v8functionwrapper.cpp:
#include "v8functionwrapper.h"
#include "v8value.h";
v8sharp::V8FunctionWrapper::V8FunctionWrapper(v8::Persistent<v8::Value> value)
{
// is this wrong?
this->_value = &value;
// still true
Console::WriteLine("Constructor: {0}", (*this->_value)->IsFunction());
}
// This function is called by C# and triggers the javascript function
Object^ v8sharp::V8FunctionWrapper::Call([ParamArray]array<Object ^> ^ args)
{
// Get a refence to the function
Console::WriteLine("Cast 2");
// MEMORY VIOLATION: the _value no longer points to a valid object :(
Console::WriteLine("IsFunction: {0}", (*this->_value)->IsFunction());
Console::ReadLine();
-- snip --
}
v8functionwrapper.h:
#pragma once
#include "v8.h"
using namespace System;
using namespace System::Reflection;
namespace v8sharp {
public ref class V8FunctionWrapper
{
public:
delegate bool V8FunctionCallback(array<Object ^> ^ args);
Object^ v8sharp::V8FunctionWrapper::Call([ParamArray]array<Object ^> ^ args);
v8::Persistent<v8::Value> Unwrap();
V8FunctionWrapper(v8::Persistent<v8::Value> value);
~V8FunctionWrapper();
!V8FunctionWrapper();
private:
V8FunctionCallback^ _callback;
v8::v8<Persistent::Value>* _value;
};
}
从这一行可以明显看出(调试代码): Console::WriteLine("IsFunction: {0}", (*this->_value)->IsFunction());
指针 _value 不再有效并导致异常。为什么我的指针无效?是因为我指向构造函数中的一个参数并且该参数被删除了吗?如果是这样,我如何获得一个不会消失的指向它的指针。请记住,这是一个 .net 类,因此我无法将本机类型混合和匹配到其中。
I'm trying to add functionality to the v8sharp project and I'm having some issues (I'm not very good at C++-CLI so I'm pretty sure the issue lies in my lack of C++-CLI abilities rather than misusing v8.)
v8value.cpp:
v8sharp::V8FunctionWrapper^ V8ValueWrapper::WrapFunction(v8::Handle<v8::Value> value) {
// Now we use the wrapper to make this safe to use
// this works
Console::WriteLine("IsFunction-First: {0}", value->IsFunction());
// persistent so it doesn't get garbage collected
v8::Persistent<v8::Value> pval(value);
// create a function wrapper
V8FunctionWrapper^ bla = gcnew V8FunctionWrapper(pval);
return bla;
}
Which should take a v8Handle<v8::Value>
which contains a function (it always will because of what calls this function) and returns a nice .net wrapper so we can use it in my C# project.
The problem lies here
v8functionwrapper.cpp:
#include "v8functionwrapper.h"
#include "v8value.h";
v8sharp::V8FunctionWrapper::V8FunctionWrapper(v8::Persistent<v8::Value> value)
{
// is this wrong?
this->_value = &value;
// still true
Console::WriteLine("Constructor: {0}", (*this->_value)->IsFunction());
}
// This function is called by C# and triggers the javascript function
Object^ v8sharp::V8FunctionWrapper::Call([ParamArray]array<Object ^> ^ args)
{
// Get a refence to the function
Console::WriteLine("Cast 2");
// MEMORY VIOLATION: the _value no longer points to a valid object :(
Console::WriteLine("IsFunction: {0}", (*this->_value)->IsFunction());
Console::ReadLine();
-- snip --
}
v8functionwrapper.h:
#pragma once
#include "v8.h"
using namespace System;
using namespace System::Reflection;
namespace v8sharp {
public ref class V8FunctionWrapper
{
public:
delegate bool V8FunctionCallback(array<Object ^> ^ args);
Object^ v8sharp::V8FunctionWrapper::Call([ParamArray]array<Object ^> ^ args);
v8::Persistent<v8::Value> Unwrap();
V8FunctionWrapper(v8::Persistent<v8::Value> value);
~V8FunctionWrapper();
!V8FunctionWrapper();
private:
V8FunctionCallback^ _callback;
v8::v8<Persistent::Value>* _value;
};
}
Evident from this line (debug code):Console::WriteLine("IsFunction: {0}", (*this->_value)->IsFunction());
The pointer _value is no longer valid and causes an exception. Why does my pointer invalidate? Is it because I'm pointing to an argument in the constructor and that gets deleted? If so how do I get a pointer to it that won't disappear. Keep in mind this is a .net class so I can't mix and match native types into it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要实例化一个新的 v8::Persistent 值作为类的成员,因为您传入的值是在堆栈上创建的,并且一旦 WrapFunction 返回就会被销毁。另外,当你的对象被销毁时,不要忘记删除 _value 。
You need to instantiate a new v8::Persistent value as a member of your class because the one you are passing in is created on the stack and will be destroyed as soon as WrapFunction returns. Also don't forget to delete _value when your object is destroyed.