Results 1 to 6 of 6

Thread: Calling QSqlDatabase::addDatabase from not main thread causes SIGSEGV

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calling QSqlDatabase::addDatabase from not main thread causes SIGSEGV

    Show how are You using this class.

  2. #2
    Join Date
    Apr 2015
    Posts
    8
    Thanks
    3

    Default Re: Calling QSqlDatabase::addDatabase from not main thread causes SIGSEGV

    Qt Code:
    1. ///////////////////////////////////////////////////////// MAIN.CPP
    2.  
    3. PostgresDriver* driver = new PostgresDriver("localhost" , 5432 , "database" , "user" , "user" , "main");
    4.  
    5. QListIterator<QSqlRecord> i( driver->executeQuery( "SELECT * FROM WHATEVER" ) );
    6. while( i.hasNext() ){
    7. }
    8. driver->deleteLater();
    To copy to clipboard, switch view to plain text mode 
    Works perfectly.

    Now:

    Qt Code:
    1. ///////////////////////////////////////////////////////// AGENT.H
    2.  
    3. class Agent : public QObject
    4. {
    5. Q_OBJECT
    6. public:
    7. Agent(QString class_name = "Agent");
    8. ~Agent();
    9.  
    10. // GETTERS
    11. unsigned int getId();
    12. QString getClass();
    13.  
    14. slots:
    15. virtual void start();
    16. virtual void end(){this->deleteLater();}
    17.  
    18. private:
    19.  
    20. // INCREMENTAL FOR IDS
    21. static unsigned int counter;
    22.  
    23. unsigned int id;
    24. QString class_name;
    25. };
    26.  
    27. ///////////////////////////////////////////////////////// AGENT.CPP
    28.  
    29. #include "Agent.h"
    30.  
    31. unsigned int Agent::counter = 0;
    32.  
    33. Agent::Agent(QString class_name) : QObject()
    34. {
    35. this->id = ++Agent::counter;
    36. this->class_name = class_name;
    37. this->setObjectName( QString("%1%2").arg( this->getClass() ).arg( this->getId() ) );
    38. }
    39.  
    40. Agent::~Agent() {
    41. this->thread()->deleteLater();
    42. }
    43.  
    44. unsigned int Agent::getId(){
    45. return this->id;
    46. }
    47.  
    48. QString Agent::getClass(){
    49. return this->class_name;
    50. }
    51.  
    52. void Agent::start(){
    53. PostgresDriver* db = new PostgresDriver("localhost" , 5432 , "database" , "user" , "user" , this->objectName() ); //CRASHES
    54. }
    55.  
    56. ///////////////////////////////////////////////////////// MAIN.CPP
    57.  
    58. Agent* agent = new Agent("Car");
    59. QThread *thread = new QThread( );
    60. agent->moveToThread( thread );
    61. agent->connect( thread, SIGNAL( started() ), agent, SLOT( start() ) );
    62. thread->start();
    To copy to clipboard, switch view to plain text mode 
    Crashes when creating a PostgresDriver inside the Agent.

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Calling QSqlDatabase::addDatabase from not main thread causes SIGSEGV

    Why don't you create the connection from your thread? i.e. your start slot?

Similar Threads

  1. Replies: 1
    Last Post: 28th March 2012, 18:58
  2. Replies: 0
    Last Post: 2nd December 2011, 19:52
  3. Replies: 5
    Last Post: 22nd February 2011, 21:21
  4. Replies: 9
    Last Post: 28th November 2009, 20:31
  5. Replies: 16
    Last Post: 7th October 2009, 08:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.