Results 1 to 9 of 9

Thread: Issue emitting signal from background thread.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue emitting signal from background thread.

    Qt Code:
    1. void MainWindow::startup()
    2. // Get pointer to layout manager for main window
    3. QLayout* layout = this->layout();
    4. // Add LCD screen to the main window layout at specified position
    5. LCD = new(IPR5000_LCD)(this);
    6. LCD->setGeometry(430,50,320,240);
    7. LCD->setParent(this);
    8. layout->addWidget(LCD);
    9.  
    10. KeyManagementAgent key_management_agent;
    11. SettingsFileRetrievalAgent settings_agent( key_management_agent );
    12. connect(&settings_agent, SIGNAL(updateSettingsNotification()), LCD, SLOT(restarting()));
    13. // move it to its own thread and do stuff with it.
    14. QThread *settingsFileUpdaterThread = new QThread;
    15. settings_agent.moveToThread(settingsFileUpdaterThread);
    16. settingsFileUpdaterThread->start();
    17. QMetaObject::invokeMethod(&settings_agent, "worker_thread", Qt::QueuedConnection);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void IPR5000_LCD::restarting()
    2. {
    3. ui->stackedWidget->setCurrentIndex(2);
    4. }
    To copy to clipboard, switch view to plain text mode 


    Like I said previously I do this sort of thing other places without problems so I don't see any reason there would be a problem with the 'LCD', it just happens to be this thread that is giving me issues.

    Commenting out the code in the restarting() method still results in a segfault. I tried printing a simple debug message and it still crashes. I don't think it's a problem with the LCD class. The LCD slot is never called because the signal is never emitted, that's how I know it cant be the LCD.

    Edit: It will loop about 5-8 times before it throws a segfault.

    I really appreciate the help.
    Last edited by Chris@Link; 3rd July 2012 at 16:59.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Issue emitting signal from background thread.

    1. Is 'key_management_agent' a parent for 'settings_agent' or just a parameter?
    2. settings_agent is a stack variable, which dies at the end of the constructor. I would expect moveToThread() to have a problem with that - try allocating it on the heap.
    This would also explain this==NULL.
    Like I said previously I do this sort of thing other places without problems so I don't see any reason there would be a problem with the 'LCD', it just happens to be this thread that is giving me issues.
    1.LCD was a likely candidate - since I don't know your code I wanted to rule it out.
    2.I fail to see the relevance of the claim that something worked somewhere else since obviously its not *exactly* the same, and secondly, obviously it doesn't work here - so it helps little that it does somewhere else.
    Specially when threads are in question - since alone different hardware (same software) may produce different results, or bring bugs to the surface on one system that are not see on another.
    For your own good - stop thinking "this has to work" if it doesn't - rather "I must be doing something wrong if it doesn't work".
    By the way, the code as you posted it, really requires no threads.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue emitting signal from background thread.

    Quote Originally Posted by high_flyer View Post
    1. Is 'key_management_agent' a parent for 'settings_agent' or just a parameter?
    2. settings_agent is a stack variable, which dies at the end of the constructor. I would expect moveToThread() to have a problem with that - try allocating it on the heap.
    This would also explain this==NULL.
    Upon further investigation the settings_agent and the key_management_agent was being destroyed after we finished the startup function. I don't know how I managed to overlook that but I did.

    Quote Originally Posted by high_flyer View Post
    2.I fail to see the relevance of the claim that something worked somewhere else since obviously its not *exactly* the same, and secondly, obviously it doesn't work here - so it helps little that it does somewhere else.
    Specially when threads are in question - since alone different hardware (same software) may produce different results, or bring bugs to the surface on one system that are not see on another.
    For your own good - stop thinking "this has to work" if it doesn't - rather "I must be doing something wrong if it doesn't work".
    By the way, the code as you posted it, really requires no threads.
    I understand, I was extremely frustrated and I overlooked the fact that the stack variable was being destroyed.


    Thanks for the help.

Similar Threads

  1. Emitting signals in other thread
    By mvbhavsar in forum Newbie
    Replies: 9
    Last Post: 8th June 2011, 08:17
  2. Replies: 5
    Last Post: 22nd February 2011, 21:21
  3. Replies: 9
    Last Post: 28th November 2009, 20:31
  4. Emitting signal from DLL to EXE
    By Miihkali in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2009, 08:32
  5. Emitting signal causes CRASH
    By navi1084 in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2009, 16:17

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
  •  
Qt is a trademark of The Qt Company.