[SOLVED] QVector issue
-
Hello all,
I have a class and inside of this class I create a thread in which I capture the desktop screen all the time. This thread puts the data into a QVector. The application runs ok but whenever I exit the application, the code crashes because the QVector no longer exists while the thread is still alive. My idea is to check if the vector is still reachable, if so put the data, if not stop the capturing. However, I can't check the existence of the vector since it will be already destroyed when I am supposed the check. What is the most proper way for this? Thanks.
-
If i understand this correctly, it looks like a fundamental flaw in the design of your application.
You start an application which allocates memory (in your case a QVector).
You start a thread which writes to your memory (QVector).
Then stop the application without caring about what is happening with your thread.That does not sound healthy at all. The result is the crash.
What you are thinking of is more a band-aid avoiding the crash. Which is certainly a way, but where does it end? What is your intention with the thread afterwards? You want to let it run forever?
If this is really the case, you need to allocate the memory in your thread. So it would not be deleted when the application stops.
From my view point a strange design, but may be you have reasons for this. You are the only one knowing this.
Probably you do not intend to let the thread running forever, so you need to stop it prior to stop the application. Either you have a mechanism to jump out of the procedure or you may use the "terminate slot":http://qt-project.org/doc/qt-5.0/qtcore/qthread.html#terminate
-
It was my bad. The code was never entering the destructor of the class where I create the thread for a known reason. Now, I call the stop function in the destructor and everything works perfect. Thanks a lot.
-
Good to know that you have solved the problem.