The problem of crash of function pointer passed in function
-
I have a Create function, the parameter is a LONG type function pointer, Create(OnMessage_pointer), this OnMessage function has 1 LONG type parameter, when the mouse moves across the window, the OnMessage function will be triggered, only once each time, and then The program crashes. My guess is that the pointer (OnMessage_pointer) may be available the first time, but invalid the second time. The following is my simplified code:
.h public: Widget(QWidget *parent = nullptr); ~Widget(); typedef void(*callback)(long,long,long); callback globalcallback = nullptr; private slots: static void OnMessage(long nMessage, long wParam, long lParam); .cpp QPoint callback; LONG callback_address; QWidget *this1; Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { LONG_PTR PlayerWinID = ui->Player->winId(); hDll = LoadLibrary(TEXT("D:/xxx.dll")); if(hDll){ typedef LONG_PTR(__stdcall *dll_CreatePlayer)(LONG_PTR,LONG,LONG,LONG, LONG , LONG , LONG , LONG , LONG ,LONG , LONG , LONG ,LONG); globalcallback = &Widget::OnMessage; callback_address = reinterpret_cast<long>(globalcallback); APlayer_hwnd = APlayer_Create(PlayerWinID,0,0,pw,ph,callback_address,0,0,0,0,0,0,0); void Widget::OnMessage(long nMessage, long wParam, long lParam){ this1->setWindowTitle("nMessage:"+QString::number(nMessage));}
Maybe I'm missing something, but this should be enough for my problem, it only succeeds once, it should have been many times in a row, but after just one time, the program crashes。
Also, a very sincere question, I cannot successfully load other dlls that this dll depends on when using debug or Release debugging mode, but there is no problem in running the exe file directly in the Release directory (I am not saying that mentioned above to this question), why is this? I am new to Qt Creator and have been searching for this problem for a long time. Please reply to me if I should add more content! Thanks!
-
I have a Create function, the parameter is a LONG type function pointer, Create(OnMessage_pointer), this OnMessage function has 1 LONG type parameter, when the mouse moves across the window, the OnMessage function will be triggered, only once each time, and then The program crashes. My guess is that the pointer (OnMessage_pointer) may be available the first time, but invalid the second time. The following is my simplified code:
.h public: Widget(QWidget *parent = nullptr); ~Widget(); typedef void(*callback)(long,long,long); callback globalcallback = nullptr; private slots: static void OnMessage(long nMessage, long wParam, long lParam); .cpp QPoint callback; LONG callback_address; QWidget *this1; Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { LONG_PTR PlayerWinID = ui->Player->winId(); hDll = LoadLibrary(TEXT("D:/xxx.dll")); if(hDll){ typedef LONG_PTR(__stdcall *dll_CreatePlayer)(LONG_PTR,LONG,LONG,LONG, LONG , LONG , LONG , LONG , LONG ,LONG , LONG , LONG ,LONG); globalcallback = &Widget::OnMessage; callback_address = reinterpret_cast<long>(globalcallback); APlayer_hwnd = APlayer_Create(PlayerWinID,0,0,pw,ph,callback_address,0,0,0,0,0,0,0); void Widget::OnMessage(long nMessage, long wParam, long lParam){ this1->setWindowTitle("nMessage:"+QString::number(nMessage));}
Maybe I'm missing something, but this should be enough for my problem, it only succeeds once, it should have been many times in a row, but after just one time, the program crashes。
Also, a very sincere question, I cannot successfully load other dlls that this dll depends on when using debug or Release debugging mode, but there is no problem in running the exe file directly in the Release directory (I am not saying that mentioned above to this question), why is this? I am new to Qt Creator and have been searching for this problem for a long time. Please reply to me if I should add more content! Thanks!
@QtCaiJi said in The problem of crash of function pointer passed in function:
static void OnMessage
Make your slot non-static and try again.
Could be already fix the issue.Where do you assign
this1
? Check ifnullptr
in slot and see if it also crashes -
I have a Create function, the parameter is a LONG type function pointer, Create(OnMessage_pointer), this OnMessage function has 1 LONG type parameter, when the mouse moves across the window, the OnMessage function will be triggered, only once each time, and then The program crashes. My guess is that the pointer (OnMessage_pointer) may be available the first time, but invalid the second time. The following is my simplified code:
.h public: Widget(QWidget *parent = nullptr); ~Widget(); typedef void(*callback)(long,long,long); callback globalcallback = nullptr; private slots: static void OnMessage(long nMessage, long wParam, long lParam); .cpp QPoint callback; LONG callback_address; QWidget *this1; Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { LONG_PTR PlayerWinID = ui->Player->winId(); hDll = LoadLibrary(TEXT("D:/xxx.dll")); if(hDll){ typedef LONG_PTR(__stdcall *dll_CreatePlayer)(LONG_PTR,LONG,LONG,LONG, LONG , LONG , LONG , LONG , LONG ,LONG , LONG , LONG ,LONG); globalcallback = &Widget::OnMessage; callback_address = reinterpret_cast<long>(globalcallback); APlayer_hwnd = APlayer_Create(PlayerWinID,0,0,pw,ph,callback_address,0,0,0,0,0,0,0); void Widget::OnMessage(long nMessage, long wParam, long lParam){ this1->setWindowTitle("nMessage:"+QString::number(nMessage));}
Maybe I'm missing something, but this should be enough for my problem, it only succeeds once, it should have been many times in a row, but after just one time, the program crashes。
Also, a very sincere question, I cannot successfully load other dlls that this dll depends on when using debug or Release debugging mode, but there is no problem in running the exe file directly in the Release directory (I am not saying that mentioned above to this question), why is this? I am new to Qt Creator and have been searching for this problem for a long time. Please reply to me if I should add more content! Thanks!
@QtCaiJi It's hard to tell what's wrong because you seemingly removed lines of actual code at random.
this1
is never assigned in that snippet.dll_CreatePlayer
is not used. Type LONG is a signed 32bit integer, so reinterpreting a function pointer to it sounds horrible and will not do what you think on e.g. 64bit system.
As to the problem at hand - if it crashes then run it under a debugger and see the callstack. We can't help if we don't know what the problem is.I cannot successfully load other dlls that this dll depends on [..], why is this?
When you run by starting the exe directly the working directory for your app is that exe's location.
When running from Qt Creator it's usually one directory up from where the exe is located (unless you change it in the project run settings). Depending on where your dll and its dependents are located they simply might not be in a location Windows looks for them. This exposes a problem in your application that it depends on a working directory path to load its dependents. It shouldn't do that as you can't predict how the end user will start your app (a shortcut, command line etc.) and so you can't predict what the working directory will be. You should structure your app so that dependencies are automatically picked up according to standard lookup rules or, if you load them manually, are using paths relative to your executable, not working directory.