PDA

View Full Version : Multi thread problem



sreedhar
24th June 2006, 10:08
Hi,
My app. uses two threads, one to run the kernel(shell) and the other is gui.
If a long running process is started in GUI, I go to the shell and delete the pointers used by my GUI, the app. crashes.
So, Is there a clean way(except from copying everything to gui, as the data structures are heavy), to stop the app. from crashing.

please suggest the possible solutions

Thanking you,

regards,
sree

jacek
24th June 2006, 12:36
Why do you have to delete those pointers? Maybe the GUI should be responsible for cleaning up the memory?

sreedhar
26th June 2006, 12:01
there will be some case when i have to delete the complete pointers in gui and set them fresh, if some things are changed from shell.

Then if an operation is already running in gui, the gui crashes

jacek
26th June 2006, 12:25
Then if an operation is already running in gui, the gui crashesSo you have to either wait for the GUI to finish or make the GUI delete those objects when it doesn't need them anymore.

ball
26th June 2006, 13:33
http://doc.trolltech.com/4.1/qobject.html#deleteLater

for any object that are extend from QObject, use the deleteLater function rather than directly use the [delete] operator

eriwik
26th June 2006, 13:33
One solution would be to reference-count the objects that both the shell and the GUI will need to access, and instead of simply calling delete on them you check how many others are refering to it. If it's 0 you delete, if not you just decrement the reference count. So when whatever happens in the shell and you need to delete the pointers you first check if they are used and if not go ahead and delete, but if they are you just create new objects.

You might also be interested in QPointer, QSharedDataPointer or one of the Implicitly Shared Classes (http://doc.trolltech.com/4.1/shclass.html#shared-classes).