Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. The problem of crash of function pointer passed in function
Forum Updated to NodeBB v4.3 + New Features

The problem of crash of function pointer passed in function

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 229 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QtCaiJi
    wrote on last edited by
    #1

    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!

    Pl45m4P Chris KawaC 2 Replies Last reply
    0
    • Q QtCaiJi

      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!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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 if nullptr in slot and see if it also crashes


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • Q QtCaiJi

        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!

        Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved