开发者

memory access violation from static callback function

I have a C++ DLL which I give a pointer to a static function as a callback:

开发者_如何学Python
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!


Is m_obj set to a live object? – sharptooth

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜