Fixed socket deletion error#59
Conversation
deleting the socket in the destructor can cause a crash when shutting down the app. By moving it to the socketDisconnected and deleting it with "deleteLater" fixed the issue
src/qhttpconnection.cpp
Outdated
| { | ||
| deleteLater(); | ||
| invalidateRequest(); | ||
| m_socket->deleteLater(); |
There was a problem hiding this comment.
Thank you for the patch. Please fix the indentation here.
Also, I do not understand why this segfaults in the dtor? Is it because m_socket is already being freed somewhere else?
|
I'm not completly shure where the crash comes from, but I do know the circumstances: If there is an open connection when the app quits, the original line The fact that it only happens while quitting lets me assume the "something" happens between the |
|
I think this crash must be solved too. I have a little bit of different solution below. |
|
The socket is deleted by the parent. The library is double deleting the object. It's explained in the documentation of |
|
https://stackoverflow.com/questions/28820450/how-to-safely-delete-a-qtqtcpsocket Deleting a socket obtained from QTcpServer seems to be correctly handled. |
|
Not if the parent QTcpServer is deleted first, then deletes its children, then the socket gets double deleted by the library. The problem is not QTcpServer doing double deletion (once deleted, the parent gets notified and removes it from its children list automatically), is QHttpServer doing it. I can't tell with 100% certainty that QHttpServer is being used right, but I think it is. I was working in our project, used Valgrind to check for possible leaks in one part of the code, then it reported a leak in our class, which uses QSimpleRestServer, which in turn uses QHttpServer. Then I ended up fixing the leak in the other pull request and the double deletion detected here. Adding some logging I get this (the connection is established, then the application is closed while it's on): In another case (the connection times out, and after a while I close the application) I have this instead: |
|
Thank you everybody for the input, and @suy for the clear explanation! I'll merge this. Again, I haven't done Qt development in 6-7 years, so I'm not sure how this specific change fixes the problem @suy outlined, because isn't there now a double-free happening in socketDisconnected? Or is it guaranteed that that will be signaled prior to QTcpServer deletion? Or is this something to do with calling |
|
Just to clarify: I did not apply this patch verbatim to our copy in our codebase, because I found this PR later. I wrapped the socket in a FYI, |
Deleting the socket in the destructor can cause a crash when shutting down the app. By moving it to the
socketDisconnectedand deleting it withdeleteLaterfixed the issue.