静态回调函数的内存访问冲突
我有一个 C++ DLL,我给它一个指向静态函数的指针作为回调:
static void CallBackFunc(int num);
为了让这个函数访问我的代码的其余部分,我在头文件中
static void* m_obj;
和代码中都有一个指向类的指针:
void* CPPUDlg::m_obj;
在静态函数中我有的函数:
CPPUDlg* m_pointer = (CPPUDlg*)m_obj;
m_pointer->OnSerialMsg(num);
这允许回调调用非静态函数,但是当该函数调用应用程序中其他类中的其他函数时,任何非静态变量都会出现内存访问冲突。
谁能发现我可能做错了什么?我是否需要所有类的静态实例或类似的东西?
谢谢!
I have a C++ DLL which I give a pointer to a static function as a callback:
static void CallBackFunc(int num);
In order for this function to access the rest of my code I have a pointer to the class in the header:
static void* m_obj;
and in the code:
void* CPPUDlg::m_obj;
and in the static function I have:
CPPUDlg* m_pointer = (CPPUDlg*)m_obj;
m_pointer->OnSerialMsg(num);
This allows the callback to call a non-static function but when this function calls other functions in other classes in the applcation it goes wrong with a memory access violation for any variables which are not static.
Can anyone spot what I may have done wrong? Do I need static instances of all my classes or something along these lines?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
非常感谢您的评论。
我检查了一下,我忘记设置
m_obj
在初始化中,我现在设置
m_obj = this;
并且一切正常!
谢谢!
Thank you so much for that comment.
I checked and I had forgotten to set
m_obj
In my initialisation I now set
m_obj = this;
and everything works perfectly!
Thanks!