Results 1 to 7 of 7

Thread: QHash, QList - are destructors called on remove?

  1. #1
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QHash, QList - are destructors called on remove?

    Suppose you have

    A: QList<myClass> myClassList;
    B: QList<myClass *> myClassList;

    When a remove is called (removeOne or clear, etc.) in cases A & B, are destructors called for myClass? Does the answer differ if using QHash or QQueue?

    I thought the destructor was called in B, but not A .

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash, QList - are destructors called on remove?

    The destructor is called in case A (in case you have objects in your container) and not in case B most likely because it's wrong to call delete on a pointer if you only assume it was used to allocate heap memory (that pointer could be pointing to stack memory).

    So if you use container of pointers and allocate heap memory for objects you need to delete that memory before each element is removed from container and delete memory for all elements before the container goes out of scope.

    //i don't see a reason for you to use a QHash or a QMap of pointers, but maybe i just didn't find it yet

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

    Default Re: QHash, QList - are destructors called on remove?

    if myClass inherits QObject then pointers have to be used.
    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.


  4. The following user says thank you to wysota for this useful post:

    Zlatomir (21st March 2011)

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QHash, QList - are destructors called on remove?

    @Wysota: is this only because QObjects are not "copyable" (since each QObject has unique meta-data and private copy constructor and assignment operator) or there are other reasons that i don't get?
    Anyway thanks for the correction

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

    Default Re: QHash, QList - are destructors called on remove?

    This is because of the reason why QObjects are not copiable. This is because they are "identity based" and not "value based". Each QObject is a separate and complete "citizen" with its own "personality" - that's why you can't copy it. Ans since you can't copy it, you can't put it into a container that requires its values to be well... values and not identities.
    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.


  7. #6
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHash, QList - are destructors called on remove?

    The objects I'm managing (well, trying to manage) are QTcpSockets. So they're QObjects. I log them into a QList so that I can shut them down from my server if it exits before the sockets are disconnected and deleted.

    Somewhere I've got a bug and it seems to be when I remove a socket from the list. I thought maybe the QList was calling the destructor -- which I wasn't counting on -- but it seems that it won't because the QList is just holding a pointer. So I'll keep hunting. Thanks.

  8. #7
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QHash, QList - are destructors called on remove?

    Reworked the socket server/handler without any collections -- only signals/slots --- and all working fine for now.

Similar Threads

  1. QList < QHash < int, QString> > ... clear() is slow
    By JoZCaVaLLo in forum Qt Programming
    Replies: 8
    Last Post: 15th March 2011, 11:07
  2. Remove widget from a QList
    By Eos Pengwern in forum Newbie
    Replies: 4
    Last Post: 16th October 2009, 21:25
  3. Can QHash::capacity() be smaller than QHash::size()?!?
    By iw2nhl in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2007, 01:17
  4. Remove first n elements from QList
    By pakulo in forum Qt Programming
    Replies: 8
    Last Post: 4th June 2007, 07:27
  5. Query about QHash , QList
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2006, 09:04

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.