Results 1 to 2 of 2

Thread: QThread: Destroyed while thread is still running

  1. #1
    Join Date
    May 2021
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Post QThread: Destroyed while thread is still running

    Hi all;

    I'm new student in programming and Qt.

    I'm trying to do some threads, I already have done some before in the same application, but now I have a problem, I did everything like I have done before, and now my threat present this problem, it destroy it while is running.
    I don't know the reason why because as I said, I have done everything like the others threads.
    I really appreciate if someone can help me.

    I will leave the code below:

    storkthread.h

    Qt Code:
    1. #ifndef STORKTHREAD_H
    2. #define STORKTHREAD_H
    3. #include "structures.h"
    4. #include <QThread>
    5.  
    6. class StorkThread : public QThread
    7. {
    8. public:
    9.  
    10. //Atributtes
    11. int sleepTime;
    12. bool running;
    13. StorkQueue * waitingQueue, * deliveringQueue;
    14. Queue * warehouseQueue;
    15. Stork * myStork;
    16. NodeStork * myNodeStork;
    17.  
    18. //Constructor
    19. void __init__(StorkQueue * waitingQueue, StorkQueue * deliveringQueue, Queue * warehouseQueue, Stork * myStork);
    20.  
    21. //Methods
    22. void sendStork();
    23. void deliverBabies();
    24. void getStorkBack();
    25. void setSleepTime(int sleep);
    26. void takeAndGiveBabies();
    27. void run();
    28. };
    29.  
    30. #endif // STORKTHREAD_H
    To copy to clipboard, switch view to plain text mode 


    storkthread.cpp
    Qt Code:
    1. #include "storkthread.h"
    2.  
    3. //Contructor, initialize the atributtes
    4. void StorkThread :: __init__(StorkQueue *waitingQueue, StorkQueue *deliveringQueue, Queue * warehouseQueue, Stork * myStork){
    5. this->waitingQueue = waitingQueue;
    6. this->deliveringQueue = deliveringQueue;
    7. this->warehouseQueue = warehouseQueue;
    8. this->myStork = myStork;
    9. this->myNodeStork = new NodeStork(myStork);
    10. this->running = true;
    11. }
    12.  
    13. //Send stork: when the stork on the queue is ready to fly this method give to the stork the type of baby and send it to delivery
    14. void StorkThread :: sendStork(){
    15. if (!waitingQueue->isEmpty()){
    16. while (true) {
    17. //Evaluate the long of the stork
    18. if (waitingQueue->first->stork->localBabies->getLong() < waitingQueue->first->stork->localBabies->queueLimit){
    19. if (warehouseQueue->isEmpty())
    20. break;
    21. //Enqueue in the stork
    22. waitingQueue->first->stork->localBabies->enqueue(warehouseQueue->dequeue()->typeOfFeeling);
    23. }
    24.  
    25. else
    26. break;
    27. }
    28. //Move the waiting stork queue to the deliviring storl queue
    29. waitingQueue->first->stork->localBabies->print();
    30. deliveringQueue->enqueue(waitingQueue->dequeue());
    31.  
    32. }
    33. }
    34.  
    35. //Deliver babies, when the stork has arrive this method dequeue everything on the stork
    36. void StorkThread :: deliverBabies(){
    37. NodeStork * localStork = deliveringQueue->dequeue();
    38. localStork->stork->deliverBabies();
    39.  
    40. waitingQueue->enqueue(localStork);
    41. }
    42.  
    43. //This method set the sleeptime
    44. void StorkThread :: setSleepTime(int sleep){
    45. this->sleepTime = sleep;
    46. }
    47.  
    48. //This method combines the previus methods
    49. void StorkThread :: takeAndGiveBabies(){
    50. while (running) {
    51. //Send the stork
    52. sendStork();
    53.  
    54. //Deliver the babies
    55. deliverBabies();
    56.  
    57.  
    58.  
    59. }
    60.  
    61.  
    62. }
    63.  
    64. //Principal Method
    65. void StorkThread :: run(){
    66. while (true)
    67. takeAndGiveBabies();
    68. }
    To copy to clipboard, switch view to plain text mode 


    And again, I really appreciate if someone can help me.
    Thanks.
    Last edited by d_stranz; 20th May 2021 at 22:37. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,238
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QThread: Destroyed while thread is still running

    You are missing the Q_OBJECT macro in the definition of the StorkThread class for one thing. That will prevent any of the signals and slots from being implemented.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 4th October 2012, 14:49
  2. QThread not running in different thread
    By lauwe in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2011, 23:02
  3. Destroyed while process is still running
    By qtzcute in forum Qt Programming
    Replies: 5
    Last Post: 23rd July 2009, 08:26
  4. Replies: 4
    Last Post: 26th June 2008, 18:41
  5. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 06:27

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.