PDA

View Full Version : Problem closing a QMainWindow in Qt4.2



ian
9th October 2006, 09:22
Environment: Qt4.2 patched to use Visual Studio 2003 integration on WindowsXP

I have just ported some code to Qt4.2 and am having a problem closing the QMainWindow. When I immediately click the close window 'x' button after launch the application throws an unhandled exception. However, if I click some widgets in the workspace area and then click the main windows 'x' button the application closes as expected. I have tried overriding the QMainWindow::close() event handler, setting attributes such as WA_DeleteOnClose and various other things all to no avail. Has anyone else come across this problem before?

jpn
9th October 2006, 09:30
Run the application in VS in debug mode, break it when the exception is thrown, and see the call stack (Debug->Windows->Call Stack). It should help you find the actual problem.

Mike
9th October 2006, 10:46
I had the problem when using QMainWindow->setAttribute(Qt::WA_DeleteOnClose); I did remove that from my code, and now the application terminates without exception.

jpn
9th October 2006, 11:15
I had the problem when using QMainWindow->setAttribute(Qt::WA_DeleteOnClose); I did remove that from my code, and now the application terminates without exception.
The QMainWindow object would get destructed twice if it's created on the stack and has that attribute set (first when closing and secondly when going out of scope). Maybe this was the case for you? This is not the case for ian, though, because for him it's working sometimes and sometimes not.

Mike
9th October 2006, 13:44
Yes you're right! I have my MainWindow on the Stack of the main function. That does explain it! Thanks. I was wondering about that, but after the problem went away I didn't bother to investigate any further.

ian
10th October 2006, 01:06
Run the application in VS in debug mode, break it when the exception is thrown, and see the call stack (Debug->Windows->Call Stack). It should help you find the actual problem.

I looked at this already. Here is the call stack after the exception is thrown.
ntdll.dll!7c90eb74()
ntdll.dll!7c90eb74()
> QtCored4.dll!QBasicAtomic::testAndSetRelease(int expected=1, int newval=0) Line 99 + 0x18 C++
QtCored4.dll!QMutex::unlock() Line 250 + 0xe C++
QtCored4.dll!qt_adopted_thread_watcher_function(vo id * __formal=0x1020301f) Line 152 C++
01fcffb4()
ntdll.dll!7c91b298()
kernel32.dll!7c80b683()
ntdll.dll!7c91b298()


I had the problem when using QMainWindow->setAttribute(Qt::WA_DeleteOnClose); I did remove that from my code, and now the application terminates without exception.
My main() function creates the MainWindow on the stack and no attributes are currently being set.

It looks like one of the custom widgets on the form doesn't seem to get shutdown properly. The mysterious thing is why it works as expected when I click the widgets on the form before closing the MainWindow but it throws the exception when I close the MainWindow immediately after launch without clicking any widgets.

jpn
10th October 2006, 08:27
Are you using threads? Are you possibly modifying the GUI anyhow from an another thread than the main GUI thread?

ian
11th October 2006, 01:41
Are you using threads? Are you possibly modifying the GUI anyhow from an another thread than the main GUI thread?

Yes. However, the interaction between the threads is using the signal-slot mechanism.

The thing I don't understand is why it all works as expected when I click some widgets in the form before closing the application but it throws an exception when I close the application immediately after launch.

jpn
11th October 2006, 18:24
Maybe the problem is with cleaning up the thread(s)? The destructor of the QThread subclass is executed in the thread where the QThread object lives in (usually same thread where it was created in), not in the thread being executed in QThread::run().

ian
13th October 2006, 06:02
Maybe the problem is with cleaning up the thread(s)? The destructor of the QThread subclass is executed in the thread where the QThread object lives in (usually same thread where it was created in), not in the thread being executed in QThread::run().

This does seem to be what the problem is but I am yet to resolve it. When I click inside the form and then close the application, all threads are exiting correctly. However, when I close the applicaton immediately after launch, the application throws the error and the threads aren't exiting correctly.

If I profile the application using DependencyWalker and close the application immediately after launch I get the following:
Second chance exception 0xC0000008 (Invalid Handle) occurred in "c:\windows\system32\NTDLL.DLL" at address 0x7C90EB74 by thread 30.
Thread 2 exited with code -1073741816 (0xC0000008).
Thread 4 exited with code -1073741816 (0xC0000008).
Thread 1 exited with code -1073741816 (0xC0000008).
Thread 3 exited with code -1073741816 (0xC0000008).
Thread 9 exited with code -1073741816 (0xC0000008).
Thread 11 exited with code -1073741816 (0xC0000008).
Thread 16 exited with code -1073741816 (0xC0000008).
Thread 18 exited with code -1073741816 (0xC0000008).
Thread 24 exited with code -1073741816 (0xC0000008).

However, if I click a widget inside the form before closing, the DependencyWalker log is as follows:
Thread 29 exited with code 0 (0x0).
Thread 27 exited with code 0 (0x0).
Thread 3 exited with code 0 (0x0).
Thread 2 exited with code 0 (0x0).
Thread 30 exited with code 0 (0x0).
Thread 9 exited with code 0 (0x0).
Thread 4 exited with code 0 (0x0).
Thread 24 exited with code 0 (0x0).
Thread 11 exited with code 0 (0x0).
Thread 16 exited with code 0 (0x0).
Thread 18 exited with code 0 (0x0).

jpn
13th October 2006, 07:26
When do the threads get started? How are they cleaned up? Are they running even loops?

ian
17th October 2006, 01:49
This problem looks to be occurring due to a bug in Qt4.2 that occurs when using threads that are not QThreads. Here is the link to the reported bug in Task Tracker.

http://www.trolltech.com/developer/task-tracker/index_html?method=entry&id=134313

Thanks everyone for your suggestions.