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. Segmentation fault using .dll compiled with Qt
QtWS25 Last Chance

Segmentation fault using .dll compiled with Qt

Scheduled Pinned Locked Moved Solved General and Desktop
dllseg fault
5 Posts 2 Posters 1.0k Views
  • 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.
  • M Offline
    M Offline
    magartor
    wrote on 25 Nov 2020, 16:52 last edited by
    #1

    Hello everyone,

    I try to use a dll compile with Qt with pybind11. My dll is compose of 2 object ExampleDll and somejob. Here is the .cpp of my ExampleDll :

    #ifndef EXAMPLEDLL_H
    #define EXAMPLEDLL_H
    
    #include <iostream>
    #include <iomanip>
    #include <QString>
    #include <QObject>
    
    #include "exampleDll_global.h"
    #include "somejob.h"
    
    class Q_DECL_EXPORT ExampleDll : public QObject
    {
        Q_OBJECT
    public:
        Q_DECL_EXPORT ExampleDll();
        Q_DECL_EXPORT virtual ~ExampleDll(){
    
        };
        float Q_DECL_EXPORT exampleMult(int a, float b);
        void Q_DECL_EXPORT startThread(void);
        void Q_DECL_EXPORT printThreadResult(void);
        const std::string Q_DECL_EXPORT getRes(void);
        void Q_DECL_EXPORT setRes(const std::string);
        someJob * ptrworkerThread;
        someJob workerThread;
        std::string  resThred;
    public slots:
        void handleResults(QString res);
    };
    
    float Q_DECL_EXPORT cppmult(int a, float b);
    
    #endif // EXAMPLEDLL_H
    

    Here the .h

    #include "exampledll.h"
    
    using namespace std;
    
    Q_DECL_EXPORT  ExampleDll::ExampleDll(){
        resThred = "String init with Empty";
    
        //cout << "resThred" << resThred << endl;
        //workerThread = new someJob();
        //QObject::connect(&workerThread, &someJob::resultReady, this, &ExampleDll::handleResults);
        //connect(workerThread, &someJob::finished, workerThread, &QObject::deleteLater);
        //workerThread.start();
    }
    
    void Q_DECL_EXPORT  ExampleDll::handleResults(QString res){
        resThred = res.toStdString();
    }
    const std::string Q_DECL_EXPORT ExampleDll::getRes(void){
        return resThred;
    }
    void Q_DECL_EXPORT ExampleDll::setRes(const std::string val){
        resThred = val;
    }
    float Q_DECL_EXPORT  ExampleDll::exampleMult(int a, float b){
        float res = a*b;
        cout << std::setprecision(1) << std::fixed
             << "   QT In cppmult: int: " << a
             << " float " << b
             << " returning " << res
             << std::endl;
        return res;
    }
    
    float Q_DECL_EXPORT cppmult(int a, float b){
        float res = a*b;
        cout << std::setprecision(1) << std::fixed
             << " inshallah In cppmult: int: " << a
             << " float " << b
             << " returning= " << res
             << std::endl;
        return res;
    }
    void Q_DECL_EXPORT ExampleDll::startThread(void){
        //workerThread.start();
    }
    void Q_DECL_EXPORT ExampleDll::printThreadResult(void){
        cout << "Thread res : " << resThred << endl;
    }
    

    Here is the .cpp of the class somejob :

    #include "somejob.h"
    
    Q_DECL_EXPORT someJob::someJob() : QObject(this)// : QThread(this)
    {
    
    }
    
    Q_DECL_EXPORT someJob::~someJob(){
    
    }
    

    And here is the .h os somejob :

    #ifndef SOMEJOB_H
    #define SOMEJOB_H
    
    #include <QObject>
    #include <QThread>
    #include <QString>
    
    class Q_DECL_EXPORT someJob : public QObject //: public QThread
    {
        //Q_OBJECT
    public:
        Q_DECL_EXPORT explicit someJob();
        Q_DECL_EXPORT ~someJob();
    /*
        void run() override {
            QString result;
            for(int i = 0; i < 10000; i++){
                int j;
                for(j = 0; j < i; j++){
                    if(i%j == 0){
                        break;
                    }
                }
                if(i==j){
                    result += QString::number(i);
                }
            }
            emit resultReady(result);
        }
    
    signals:
        void resultReady(const QString &s);
    
    signals:
    */
    };
    
    #endif // SOMEJOB_H
    

    For some reason, my dll works when the line this line is commented :

    someJob workerThread;
    

    But I get a segmentation fault when using it. Here is a code I use to test the dll :

    #include "exampledll.h"
    
    int main(int argc, char * argv[]){
    	float res = cppmult(5, 3.0f);
    	if(res != 15.0){
    		return -1;
    	}
    	ExampleDll test;
    	test.exampleMult(5,6);
    	return 0;
    }
    

    The code is compiled with the following command :

    g++ -DQT_NO_VERSION_TAGGING -Wall -Werror -fPIC -std=c++11 `pkg-config --cflags --libs Qt5Core` -L ~/bind/overview_article/ -I . testcppmult.c  -L. -lexampleDll -Wl,-rpath,.
    

    Here is the configuration of Qt and the os :
    My ubuntu version is :

    Distributor ID : Ubuntu
    Description : Ubuntu 20.04.1 LTS
    Release 20.04
    Codename focal

    Qt version : 5.12.8
    Qt Creator 4.11.0

    What did I do wrong ? :/

    Sincerely,

    Magarto

    J 2 Replies Last reply 26 Nov 2020, 06:20
    0
    • M magartor
      25 Nov 2020, 16:52

      Hello everyone,

      I try to use a dll compile with Qt with pybind11. My dll is compose of 2 object ExampleDll and somejob. Here is the .cpp of my ExampleDll :

      #ifndef EXAMPLEDLL_H
      #define EXAMPLEDLL_H
      
      #include <iostream>
      #include <iomanip>
      #include <QString>
      #include <QObject>
      
      #include "exampleDll_global.h"
      #include "somejob.h"
      
      class Q_DECL_EXPORT ExampleDll : public QObject
      {
          Q_OBJECT
      public:
          Q_DECL_EXPORT ExampleDll();
          Q_DECL_EXPORT virtual ~ExampleDll(){
      
          };
          float Q_DECL_EXPORT exampleMult(int a, float b);
          void Q_DECL_EXPORT startThread(void);
          void Q_DECL_EXPORT printThreadResult(void);
          const std::string Q_DECL_EXPORT getRes(void);
          void Q_DECL_EXPORT setRes(const std::string);
          someJob * ptrworkerThread;
          someJob workerThread;
          std::string  resThred;
      public slots:
          void handleResults(QString res);
      };
      
      float Q_DECL_EXPORT cppmult(int a, float b);
      
      #endif // EXAMPLEDLL_H
      

      Here the .h

      #include "exampledll.h"
      
      using namespace std;
      
      Q_DECL_EXPORT  ExampleDll::ExampleDll(){
          resThred = "String init with Empty";
      
          //cout << "resThred" << resThred << endl;
          //workerThread = new someJob();
          //QObject::connect(&workerThread, &someJob::resultReady, this, &ExampleDll::handleResults);
          //connect(workerThread, &someJob::finished, workerThread, &QObject::deleteLater);
          //workerThread.start();
      }
      
      void Q_DECL_EXPORT  ExampleDll::handleResults(QString res){
          resThred = res.toStdString();
      }
      const std::string Q_DECL_EXPORT ExampleDll::getRes(void){
          return resThred;
      }
      void Q_DECL_EXPORT ExampleDll::setRes(const std::string val){
          resThred = val;
      }
      float Q_DECL_EXPORT  ExampleDll::exampleMult(int a, float b){
          float res = a*b;
          cout << std::setprecision(1) << std::fixed
               << "   QT In cppmult: int: " << a
               << " float " << b
               << " returning " << res
               << std::endl;
          return res;
      }
      
      float Q_DECL_EXPORT cppmult(int a, float b){
          float res = a*b;
          cout << std::setprecision(1) << std::fixed
               << " inshallah In cppmult: int: " << a
               << " float " << b
               << " returning= " << res
               << std::endl;
          return res;
      }
      void Q_DECL_EXPORT ExampleDll::startThread(void){
          //workerThread.start();
      }
      void Q_DECL_EXPORT ExampleDll::printThreadResult(void){
          cout << "Thread res : " << resThred << endl;
      }
      

      Here is the .cpp of the class somejob :

      #include "somejob.h"
      
      Q_DECL_EXPORT someJob::someJob() : QObject(this)// : QThread(this)
      {
      
      }
      
      Q_DECL_EXPORT someJob::~someJob(){
      
      }
      

      And here is the .h os somejob :

      #ifndef SOMEJOB_H
      #define SOMEJOB_H
      
      #include <QObject>
      #include <QThread>
      #include <QString>
      
      class Q_DECL_EXPORT someJob : public QObject //: public QThread
      {
          //Q_OBJECT
      public:
          Q_DECL_EXPORT explicit someJob();
          Q_DECL_EXPORT ~someJob();
      /*
          void run() override {
              QString result;
              for(int i = 0; i < 10000; i++){
                  int j;
                  for(j = 0; j < i; j++){
                      if(i%j == 0){
                          break;
                      }
                  }
                  if(i==j){
                      result += QString::number(i);
                  }
              }
              emit resultReady(result);
          }
      
      signals:
          void resultReady(const QString &s);
      
      signals:
      */
      };
      
      #endif // SOMEJOB_H
      

      For some reason, my dll works when the line this line is commented :

      someJob workerThread;
      

      But I get a segmentation fault when using it. Here is a code I use to test the dll :

      #include "exampledll.h"
      
      int main(int argc, char * argv[]){
      	float res = cppmult(5, 3.0f);
      	if(res != 15.0){
      		return -1;
      	}
      	ExampleDll test;
      	test.exampleMult(5,6);
      	return 0;
      }
      

      The code is compiled with the following command :

      g++ -DQT_NO_VERSION_TAGGING -Wall -Werror -fPIC -std=c++11 `pkg-config --cflags --libs Qt5Core` -L ~/bind/overview_article/ -I . testcppmult.c  -L. -lexampleDll -Wl,-rpath,.
      

      Here is the configuration of Qt and the os :
      My ubuntu version is :

      Distributor ID : Ubuntu
      Description : Ubuntu 20.04.1 LTS
      Release 20.04
      Codename focal

      Qt version : 5.12.8
      Qt Creator 4.11.0

      What did I do wrong ? :/

      Sincerely,

      Magarto

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Nov 2020, 06:20 last edited by
      #2

      @magartor And where exactly does it crash?
      Please post stack trace after the crash.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        magartor
        wrote on 26 Nov 2020, 08:25 last edited by
        #3

        Thank for your reply ! I change the code leading to segmentation fault by this :

        #include "exampledll.h"
        #include <execinfo.h>
        #include <signal.h>
        #include <stdlib.h>
        #include <stdio.h>
        #include <unistd.h>
        #include <iostream>
        
        using namespace std;
        
        void handler(int sig){
        	void * array[10];
        	int size;
        	size = backtrace(array, 10);
        	fprintf(stderr, "Error: signal %d:\n",sig);
        	backtrace_symbols_fd(array, size, STDERR_FILENO);
        	exit(1);
        }
        
        int main(int argc, char * argv[]){
        	signal(SIGSEGV, handler);
        	std::cout << "Main start" << std::endl;
        	float res = cppmult(5, 3.0f);
        	cout << "Res mult = " <<  res << endl;
        	if(res != 15.0){
        		return -1;
        	}
        	cout << "Instantiate class" << endl;
        	ExampleDll test;
        	cout << "Call method class" << endl;
        	test.exampleMult(5,6);
        	cout << "Return" << endl;
        	return 0;
        }
        

        Segmentation fault append when executing the line "ExampleDll test;". Here is the backtrace :

        Error: signal 11:
        ./a.out(+0x13b8)[0x55d2256e33b8]
        /lib/x86_64-linux-gnu/libc.so.6(+0x46210)[0x7f5f11c30210]
        /lib/x86_64-linux-gnu/libQt5Core.so.5(_ZNK7QObject6threadEv+0xc)[0x7f5f12287d3c]
        /lib/x86_64-linux-gnu/libQt5Core.so.5(_ZN7QObjectC2EPS_+0x4b)[0x7f5f122928eb]
        ./libexampleDll.so.1(_ZN7someJobC1Ev+0x10)[0x7f5f12537cc0]
        ./libexampleDll.so.1(_ZN10ExampleDllC1Ev+0x33)[0x7f5f12537c03]
        ./a.out(+0x1511)[0x55d2256e3511]
        /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0x7f5f11c110b3]
        ./a.out(+0x12ce)[0x55d2256e32ce]
        

        I think it come from the "someJob" instantiation, because if I modify the class to :

        class Q_DECL_EXPORT someJob //: public QObject //: public QThread
        {
            //Q_OBJECT
        public:
            Q_DECL_EXPORT explicit someJob();
            Q_DECL_EXPORT ~someJob();
        };
        
        using namespace std;
        
        Q_DECL_EXPORT someJob::someJob() //: QObject(this)// : QThread(this)
        {
            cout << "Instatiation works !" << endl;
        }
        
        Q_DECL_EXPORT someJob::~someJob(){
        
        }
        

        I don't get segmentation fault anymore. Is there some restriction with inheritance when creating dll ?

        Sincerely,

        1 Reply Last reply
        0
        • M magartor
          25 Nov 2020, 16:52

          Hello everyone,

          I try to use a dll compile with Qt with pybind11. My dll is compose of 2 object ExampleDll and somejob. Here is the .cpp of my ExampleDll :

          #ifndef EXAMPLEDLL_H
          #define EXAMPLEDLL_H
          
          #include <iostream>
          #include <iomanip>
          #include <QString>
          #include <QObject>
          
          #include "exampleDll_global.h"
          #include "somejob.h"
          
          class Q_DECL_EXPORT ExampleDll : public QObject
          {
              Q_OBJECT
          public:
              Q_DECL_EXPORT ExampleDll();
              Q_DECL_EXPORT virtual ~ExampleDll(){
          
              };
              float Q_DECL_EXPORT exampleMult(int a, float b);
              void Q_DECL_EXPORT startThread(void);
              void Q_DECL_EXPORT printThreadResult(void);
              const std::string Q_DECL_EXPORT getRes(void);
              void Q_DECL_EXPORT setRes(const std::string);
              someJob * ptrworkerThread;
              someJob workerThread;
              std::string  resThred;
          public slots:
              void handleResults(QString res);
          };
          
          float Q_DECL_EXPORT cppmult(int a, float b);
          
          #endif // EXAMPLEDLL_H
          

          Here the .h

          #include "exampledll.h"
          
          using namespace std;
          
          Q_DECL_EXPORT  ExampleDll::ExampleDll(){
              resThred = "String init with Empty";
          
              //cout << "resThred" << resThred << endl;
              //workerThread = new someJob();
              //QObject::connect(&workerThread, &someJob::resultReady, this, &ExampleDll::handleResults);
              //connect(workerThread, &someJob::finished, workerThread, &QObject::deleteLater);
              //workerThread.start();
          }
          
          void Q_DECL_EXPORT  ExampleDll::handleResults(QString res){
              resThred = res.toStdString();
          }
          const std::string Q_DECL_EXPORT ExampleDll::getRes(void){
              return resThred;
          }
          void Q_DECL_EXPORT ExampleDll::setRes(const std::string val){
              resThred = val;
          }
          float Q_DECL_EXPORT  ExampleDll::exampleMult(int a, float b){
              float res = a*b;
              cout << std::setprecision(1) << std::fixed
                   << "   QT In cppmult: int: " << a
                   << " float " << b
                   << " returning " << res
                   << std::endl;
              return res;
          }
          
          float Q_DECL_EXPORT cppmult(int a, float b){
              float res = a*b;
              cout << std::setprecision(1) << std::fixed
                   << " inshallah In cppmult: int: " << a
                   << " float " << b
                   << " returning= " << res
                   << std::endl;
              return res;
          }
          void Q_DECL_EXPORT ExampleDll::startThread(void){
              //workerThread.start();
          }
          void Q_DECL_EXPORT ExampleDll::printThreadResult(void){
              cout << "Thread res : " << resThred << endl;
          }
          

          Here is the .cpp of the class somejob :

          #include "somejob.h"
          
          Q_DECL_EXPORT someJob::someJob() : QObject(this)// : QThread(this)
          {
          
          }
          
          Q_DECL_EXPORT someJob::~someJob(){
          
          }
          

          And here is the .h os somejob :

          #ifndef SOMEJOB_H
          #define SOMEJOB_H
          
          #include <QObject>
          #include <QThread>
          #include <QString>
          
          class Q_DECL_EXPORT someJob : public QObject //: public QThread
          {
              //Q_OBJECT
          public:
              Q_DECL_EXPORT explicit someJob();
              Q_DECL_EXPORT ~someJob();
          /*
              void run() override {
                  QString result;
                  for(int i = 0; i < 10000; i++){
                      int j;
                      for(j = 0; j < i; j++){
                          if(i%j == 0){
                              break;
                          }
                      }
                      if(i==j){
                          result += QString::number(i);
                      }
                  }
                  emit resultReady(result);
              }
          
          signals:
              void resultReady(const QString &s);
          
          signals:
          */
          };
          
          #endif // SOMEJOB_H
          

          For some reason, my dll works when the line this line is commented :

          someJob workerThread;
          

          But I get a segmentation fault when using it. Here is a code I use to test the dll :

          #include "exampledll.h"
          
          int main(int argc, char * argv[]){
          	float res = cppmult(5, 3.0f);
          	if(res != 15.0){
          		return -1;
          	}
          	ExampleDll test;
          	test.exampleMult(5,6);
          	return 0;
          }
          

          The code is compiled with the following command :

          g++ -DQT_NO_VERSION_TAGGING -Wall -Werror -fPIC -std=c++11 `pkg-config --cflags --libs Qt5Core` -L ~/bind/overview_article/ -I . testcppmult.c  -L. -lexampleDll -Wl,-rpath,.
          

          Here is the configuration of Qt and the os :
          My ubuntu version is :

          Distributor ID : Ubuntu
          Description : Ubuntu 20.04.1 LTS
          Release 20.04
          Codename focal

          Qt version : 5.12.8
          Qt Creator 4.11.0

          What did I do wrong ? :/

          Sincerely,

          Magarto

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 26 Nov 2020, 08:38 last edited by
          #4

          @magartor said in Segmentation fault using .dll compiled with Qt:

          Q_DECL_EXPORT someJob::someJob() : QObject(this)

          You're setting the object as its own parent! Remove "this" and check whether it still crashes.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3
          • M Offline
            M Offline
            magartor
            wrote on 27 Nov 2020, 09:21 last edited by
            #5

            Hi,

            It works thanks :)

            1 Reply Last reply
            0

            5/5

            27 Nov 2020, 09:21

            • Login

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