Results 1 to 3 of 3

Thread: Destructor, Inheritance and Threads

  1. #1
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Lightbulb Destructor, Inheritance and Threads

    HI everybody,

    I have a small question. but first i want to say, that perhaps this is no so related to Qt.
    But perhaps( at least i hope) it helpfull in general.

    Here is some code to help to undesrtand my problem:

    Qt Code:
    1. /***/
    2. class WorkerThread;
    3.  
    4. class Father: public QObject
    5. {
    6. public:
    7. Father();
    8. ~Father();
    9. void doSome();
    10. protected:
    11. virtual qint32 function()=0;
    12. virtual void work();
    13. WorkerThread* worker;
    14. friend class WorkerThread;
    15. bool running;
    16. bool working;
    17. virtual void stop();
    18. };
    19. Father::Father()
    20. {
    21. working=false;
    22. running=true;
    23. worker=new WorkerThread(this);
    24. worker->start();
    25. }
    26. Father::~Father()
    27. {
    28. if(worker)
    29. {
    30. if(worker->IsRunning())
    31. {
    32. stop();
    33. }
    34. if (QThread::currentThread() != worker)
    35. {
    36. while (!worker->wait(2000))
    37. {
    38. }
    39. }
    40. delete worker;
    41. }
    42. }
    43. void Father::doSome()
    44. {
    45. working=true;
    46. }
    47. void Father::stop()
    48. {
    49. running=false;
    50. }
    51. void Father::work()
    52. {
    53. //other stuff
    54. qint32 a=function();
    55. }
    56. /***/
    57. classs WorkerThread: public QThread
    58. {
    59. public:
    60. WorkerThread(Father *d);
    61. protected:
    62. void run();
    63. private:
    64. Father* friend;
    65. };
    66. WorkerThread(Father *d)
    67. {
    68. friend=d;
    69. }
    70. void WorkerThread::run()
    71. {
    72. while(friend->running)
    73. {
    74. if(friend->working)
    75. {
    76. friend->working=false;
    77. friend->work();
    78. }
    79. }
    80. }
    81. /***/
    82. class Child(): public Father
    83. {
    84. public:
    85. Child();
    86. ~ Child();
    87. protected:
    88. virtual qint32 function();
    89. };
    90. Child::Child():
    91. Father()
    92. {
    93. }
    94. Child::~Child()
    95. {
    96. //empty
    97. }
    98. qint32 Child::function()
    99. {
    100. //some work;
    101. }
    102. /***/
    103. class Window:public QWidget
    104. {
    105. public:
    106. Window(QWidget*);
    107. ~Window();
    108. private:
    109. Child* pointer;
    110. private slots:
    111. void on_button1_clicked();
    112. };
    113. Window(QWidget* parent):
    114. QWidget(parent)
    115. {
    116. pointer = new Child();
    117. }
    118. Window::~Window()
    119. {
    120. if(pointer)
    121. {
    122. delete pointer;
    123. }
    124. }
    125. void Window::on_button1_clicked()
    126. {
    127. pointer->doSome();
    128. }
    129. /***/
    To copy to clipboard, switch view to plain text mode 

    i have also some mutex but i think is no need to show then here

    destructor call are done in this order
    dtr Window;
    dtr Child;
    dtr Father; which also call the dtr of worker.

    the problem appear when the worker is inside "work" and the destructor is called, then it reaches the called of function "function" (that is virtual and implemented in childs) and it crash. i have found (or better i think) that the function seems to dont exist any longer since the dtr of Child have been already called. So my solution was to called the the stop of the thread in the child. but i don like this solution since i have several child and i wiil to reimplemt (copy and paste) in all of then. Is this a normal behavior? or someone have a better idea?

    PS: This is not a running code just to explain a bit what occur to me
    CAFU......

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    509
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Destructor, Inheritance and Threads

    Hi,
    if (QThread::currentThread() != worker)
    I think this cannot work because if the program checks this condition it is per definitionem not in worker thread but in the thread where Father lives.

    Ginsengelf

  3. #3
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Destructor, Inheritance and Threads

    well this should not be false, but a extra preacaution
    CAFU......

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. No context switch between Threads and parent ?
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 29th October 2008, 22:49
  4. Once more: Threads in Qt4
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 18:35

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.