[英]c++ read access violation after casting
I have a static method inside my class, which I use for a callback. 我的类中有一个静态方法,用于回调。 Inside this callback I want to add a value to result
. 在此回调中,我想向result
添加一个值。 This seems not to be possible because of the recast(?). 由于recast(?),这似乎不可能。
Is there a way to access the member variable result
and add values to it after recasting, or do I need to think of a different way? 是否有一种方法可以访问成员变量result
并在重铸后为其添加值,还是我需要考虑一种不同的方法?
MyClass.h MyClass.h
class MyClass
{
public:
vector<string> result;
static int c_CB(void *data, int argc, char **argv, char **azColName);
int Callback(int argc, char **argv, char **azColName);
void Do(string query);
}
MyClass.cpp MyClass.cpp
void MyClass:Do(string query)
{
sqlite3_exec(this->dbResource, query.c_str(), this->c_CB , NULL, &this->errorMsg);
}
int MyClass::c_CB(void *NotUsed, int argc, char **argv, char **azColName)
{
MyClass* bar = reinterpret_cast<MyClass*>(NotUsed);
// after reinterpret cast, it does not work
//bar->result.insert(bar->result.end(), "foo");
// function call works
return bar->Callback(argc, argv, azColName);
}
int MyClass::Callback(int argc, char **argv, char **azColName)
{
cout << "working" << endl;
}
main.cpp main.cpp
int main()
{
MyClass* cl = new MyClass();
cl->Do("something");
}
The documentation states that the fourth argument to sqlite3_exec
is passed as the first argument of the callback. 文档指出,将sqlite3_exec
的第四个参数作为回调的第一个参数传递。
Currently you are passing NULL
, which you then cast to a MyClass*
and attempt access the object, which results in undefined behaviour. 当前,您正在传递NULL
,然后将其MyClass*
转换为MyClass*
并尝试访问该对象,这将导致未定义的行为。
Use this
as the fourth argument in place of NULL
. 使用this
作为第四个参数代替NULL
。
It seems you are trying to access a non-static member function from a static member callback function c_CB
. 看来您正在尝试从静态成员回调函数c_CB
访问非静态成员函数。
You were able to call a member function Callback
but nothing from the class is accessible, but if I'm correct, the compiler just interpreted Callback as a static function. 您可以调用成员函数Callback
但是无法访问该类中的任何内容,但是如果我没错,编译器只是将Callback解释为静态函数。
If you actually breakpoint inside this function, you can see that none of your member variables actually have a memory address. 如果您实际上在此函数内设置了断点,则可以看到没有任何成员变量实际具有内存地址。 And the current class, this
is null 和当前类, this
是null
I'm not sure if the cout <<
statement still works or if its causing the problem 我不确定cout <<
语句是否仍然有效或是否导致了问题
This is a possible solution to your issue, although it may not be the best one. 这可能是解决您的问题的一种方法,尽管它可能不是最佳解决方案。
class MyClass
{
public:
vector<string> result;
static int c_CB(void *data, int argc, char **argv, char **azColName);
int Callback(int argc, char **argv, char **azColName);
void Do(string query);
void MemberFunction()
{
cout << "working" << endl;
}
static MyClass* currentClass; //Add a static class pointer and assign your class address to it before firing the callback.
}
MyClass* MyClass::currentClass = NULL;
void MyClass:Do(string query)
{
sqlite3_exec(this->dbResource, query.c_str(), this->c_CB , NULL, &this->errorMsg);
}
int MyClass::c_CB(void *NotUsed, int argc, char **argv, char **azColName)
{
MyClass* bar = reinterpret_cast<MyClass*>(NotUsed);
// after reinterpret cast, it does not work
//bar->result.insert(bar->result.end(), "foo");
// function call works
return bar->Callback(argc, argv, azColName);
}
int MyClass::Callback(int argc, char **argv, char **azColName)
{
currentClass->MemberFunction();
}
int main()
{
MyClass* cl = new MyClass();
MyClass::currentClass = this;
cl->Do("something");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.