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