Results 1 to 3 of 3

Thread: QObject Automatic Signal Disconnection

  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QObject Automatic Signal Disconnection

    Hello all, I'm unsure if this is the appropriate location for this suggestion.
    I've been heavily using the QNetworkAccessManager recently for applications which are designed to run for months at a time, I noticed a memory issue which can't be properly classified as a leak.

    This small block demonstrates the issue (it could be connected to anything)

    Qt Code:
    1. QNetworkReply* reply = network_access_manager.get(request);
    2.  
    3. connect(reply, SIGNAL(finished()),
    4. &e_l , SLOT(quit()));
    5. e_l.exec();
    6.  
    7. /* ... */
    8.  
    9. reply->deleteLater();
    To copy to clipboard, switch view to plain text mode 

    After execution of this code, all allocated memory is freed...however somewhere in the QObject world there's reference to reply being connected to e_l. This remains true even if both variables are freed, at which point there's no way to remove the reference (as far as I'm aware) and so it's essentially lost memory. My very rough test found using Qt 4.8.0 Linux x86_64 that .543K was lost per connection which was not disconnected. This behaviour was unexpected to me (even if I am in the wrong) and I'd predict it to cause unexpected leaks for anyone developing with the network utilities. Is there no reason the d-tor of the freed cannot inform the paired object so this reference can be removed?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject Automatic Signal Disconnection

    Quote Originally Posted by _M_chipz View Post
    however somewhere in the QObject world there's reference to reply being connected to e_l. This remains true even if both variables are freed
    No, that's not true. There is a piece of code in QObject destructor that breaks all signal-slot connections regarding the object being destroyed.

    Qt Code:
    1. // disconnect all receivers
    2. if (d->connectionLists) {
    3. ++d->connectionLists->inUse;
    4. int connectionListsCount = d->connectionLists->count();
    5. for (int signal = -1; signal < connectionListsCount; ++signal) {
    6. QObjectPrivate::ConnectionList &connectionList =
    7. (*d->connectionLists)[signal];
    8.  
    9. while (QObjectPrivate::Connection *c = connectionList.first) {
    10. if (!c->receiver) {
    11. connectionList.first = c->nextConnectionList;
    12. delete c;
    13. continue;
    14. }
    15.  
    16. QMutex *m = signalSlotLock(c->receiver);
    17. bool needToUnlock = QOrderedMutexLocker::relock(signalSlotMutex, m);
    18.  
    19. if (c->receiver) {
    20. *c->prev = c->next;
    21. if (c->next) c->next->prev = c->prev;
    22. }
    23. if (needToUnlock)
    24. m->unlockInline();
    25.  
    26. connectionList.first = c->nextConnectionList;
    27. delete c;
    28. }
    29. }
    30.  
    31. if (!--d->connectionLists->inUse) {
    32. delete d->connectionLists;
    33. } else {
    34. d->connectionLists->orphaned = true;
    35. }
    36. d->connectionLists = 0;
    37. }
    38.  
    39. // disconnect all senders
    40. QObjectPrivate::Connection *node = d->senders;
    41. while (node) {
    42. QObject *sender = node->sender;
    43. QMutex *m = signalSlotLock(sender);
    44. node->prev = &node;
    45. bool needToUnlock = QOrderedMutexLocker::relock(signalSlotMutex, m);
    46. //the node has maybe been removed while the mutex was unlocked in relock?
    47. if (!node || node->sender != sender) {
    48. m->unlockInline();
    49. continue;
    50. }
    51. node->receiver = 0;
    52. QObjectConnectionListVector *senderLists = sender->d_func()->connectionLists;
    53. if (senderLists)
    54. senderLists->dirty = true;
    55.  
    56. node = node->next;
    57. if (needToUnlock)
    58. m->unlockInline();
    59. }
    To copy to clipboard, switch view to plain text mode 
    My very rough test found using Qt 4.8.0 Linux x86_64 that .543K was lost per connection which was not disconnected.
    How exactly did you test it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2012
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QObject Automatic Signal Disconnection

    Hello wysota. Apologies, in my test case I was using deleteLater() in a tight loop and using pmap to track memory. It caused more memory to be mapped to the process which tricked me into seeing leak-like behaviour. Changing the test methodology shows there is in fact no issue.

    This post can be closed (If such things are done here).

Similar Threads

  1. QObject signal/slot not working
    By Msnforum in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2009, 22:50
  2. QObject::connect: No such signal
    By caseyong in forum Qt Programming
    Replies: 5
    Last Post: 19th February 2008, 07:23
  3. Replies: 2
    Last Post: 6th December 2006, 23:41
  4. QSocket - signal for network wire disconnection
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 5th September 2006, 13:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.