PDA

View Full Version : QSocketNotifier Issue



TheGrimace
18th July 2007, 15:11
Is there any way to disable the default QSocketNotifier in windows? I have a Qt application that fails when stopped in Window, but succeeds in red hat. The only big difference that I can detect is the QSocketNotifier issue.

Thank You

marcel
18th July 2007, 19:34
Notes for Windows Users The socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket. The activated (http://www.qtcentre.org/forum/qsocketnotifier.html#activated)() signal is sometimes triggered by high general activity on the host, even if there is nothing to read. A subsequent read from the socket can then fail, the error indicating that there is no data available (e.g., WSAEWOULDBLOCK). This is an operating system limitation, and not a bug in QSocketNotifier.
To ensure that the socket notifier handles read notifications correctly, follow these steps when you receive a notification:

Disable the notifier.
Read data from the socket.
Re-enable the notifier if you are interested in more data (such as after having written a new command to a remote server). To ensure that the socket notifier handles write notifications correctly, follow these steps when you receive a notification:

Disable the notifier.
Write as much data as you can (before EWOULDBLOCK is returned).
Re-enable notifier if you have more data to write. Further information: On Windows, Qt always disables the notifier after getting a notification, and only re-enables it if more data is expected. For example, if data is read from the socket and it can be used to read more, or if reading or writing is not possible because the socket would block, in which case it is necessary to wait before attempting to read or write again.



So the only way of disabling that is to handle the first notification.
But I am not sure if all things will be OK if you disable it permanently.

Could you be more specific with the problem description?

Regards

TheGrimace
18th July 2007, 19:39
What I think is going on is the following:

I start the transmitting and receiving of information on their respective threads. When I stop, I wait for the QTcpServer and QTcpSocket to stop before I delete their class holders. The event loop is then given control and one extra QSocketNotifier issue is thrown. At this point, my thread is gone and I get an unhandled exception where QObject is attempting to access memory that doesn't belong to it (namely the pointer to the thread).

Edit:

How would I handle the default notifier issues? I am confused as I cannot even find out how to get a pointer to it.

marcel
18th July 2007, 20:00
OK. But I still don't understand from where the exception is coming from... From the GUI thread?

About the other thing, getting a handle of the default socket notifier... well, you can't do that.

Regards

TheGrimace
18th July 2007, 20:12
All right. I am a little confused. How can I ignore something that I can't point to?

As for the exception:

When looking at the stack trace. It appears that the first call comes from threadex() and propogates through Qt libraries until it reaches the QEventHandler which calls QSocketNotifier which calls the QObject library. Here it tries to return a pointer to a unallocated area of memory.

Thank you for the information you are providing

marcel
18th July 2007, 20:26
I do not believe the problem lies in QSocketNotifier. It must be something else.
But, as usually, your descriptions are very vague :).

If the worker thread is stopped(and deleted I assume), then why another thread's event dispatcher owns a socket notifier for your dead thread?

The socket notifier for a worker thread should be owned by the thread's event dispatcher.
Perhaps this is another thread problem.

This is as much as I can say(actually, mostly assume) based on your post.

Regards

TheGrimace
18th July 2007, 21:29
Well, I am accessing the sockets from another thread for reading and writing. The QSocketNotifier complains about this, but it still works. Afterward, I do not delete the thread that accessed these members, only the one which created them.

I apologize that I am not more clear. I would show code, but as I have stated in other posts: most of the time I am asking about code which belongs to my company and not to me. And they have issues with my posting any part of it. I can, however, try to paint a layout of the objects involved:


GUI
|
\/
Main Thread
|
\/
Another Thread
/ \
\/ \/
TCPServer TCPClient

The QTcpServer and QTcpClients are created in TCPServer and TCPClient respectively. They are accessed from "Another Thread" for reading and writing.

It is a blocking TCP design like Fortune Server Blocking example.

Everything works until I stop. When I stop. I wait for everything to disconnect and then delete TCPServer and TCPClient. and stop the run method of "Another Thread"

After all of this has happened and Qt gets the event loop back, the QSocketNotifier is called by threadex.