Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Delete operator vs ~QTreeWidgetItem()

[SOLVED] Delete operator vs ~QTreeWidgetItem()

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 4.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    XGuy
    wrote on last edited by
    #1

    Hi,
    I would like to know the difference between Delete operator and ~QTreeWidgetItem().
    In my code when i use ~QTreeWidgetItem(); no segFault will happen but if use Delete it happens!

    Here is the code which works fine:
    @
    itemRename = m_ui->trvConnections->takeTopLevelItem(m_ui->trvConnections->indexOfTopLevelItem(itemRename));
    if(itemRename)
    {
    itemRename->~QTreeWidgetItem();
    itemRename = NULL;
    }
    @

    And here is the code which leads to segFault:
    @
    itemRename = m_ui->trvConnections->takeTopLevelItem(m_ui->trvConnections->indexOfTopLevelItem(itemRename));
    if(itemRename)
    {
    delete itemRename;
    itemRename = NULL;
    }
    @

    Btw: i always(both cases) get below msg in Application output window in takeTopLevelItem() which I don't know why!
    "QAbstractItemModel::endRemoveRows: Invalid index ( 0 , 5 ) in model QTreeModel(0x30b4470) "

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mnaydenov
      wrote on last edited by
      #2

      To point the obvious, there is a huge diff b/w calling the dtor and using delete - the former does not release the memory of the item itself.

      My guess is - the item is already deleted by someone else, like for instance a parent-child relationship. An easy way to test this is a simple qDebug() (and/ or a breakpoint) in the dtor to see when and if it is called.

      Also I find it strange the fact that you assign to a pointer you used for a lookup - this can easily be messed up. Also, you don't say at what line there is a segfault.

      1 Reply Last reply
      0
      • X Offline
        X Offline
        XGuy
        wrote on last edited by
        #3

        Thanks for your reply.

        @
        // this function opens text editor for selected item inside treeview
        void Preferences::renameConn()
        {
        .
        .
        .
        // i get selected item in the list inside this function
        selectedItem->setFlags(Qt::ItemIsEditable | selectedItem->flags());
        QObject::connect(m_ui->trvConnections, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(itemRenamed(QTreeWidgetItem*,int)));
        m_ui->trvConnections->editItem(selectedItem, 2);--
        }

        /* when item text is changed i want to delete the item and create a new item with new text */
        void Preferences::itemRenamed(QTreeWidgetItem *itemRename, int col)
        {
        qDebug() << itemRename->text(2); // prints item text successfully
        m_ui->
        trvConnections->
        takeTopLevelItem(m_ui->
        trvConnections->
        indexOfTopLevelItem(itemRename));
        qDebug() << itemRename->text(2); // prints item text successfully
        if(itemRename)
        {
        delete itemRename;
        // itemRename->~QTreeWidgetItem();
        itemRename = NULL;
        }
        } // as the function ends i receive a segFault
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mnaydenov
          wrote on last edited by
          #4

          It seems the item is used by someone after the function returns and it is not save to delete it in the slot.
          Again, does the item have a parent? If yes, this will fail at some point as well.

          Also, as stated in the docs, it is not save to delete QObject directly because it might receive events later on; you should use deleteLater().

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            As side note, you can take the same TreeWidgetItem and set the text. This will save you from delete and create.

            Also when you are using delete, it may be failing to delete the data which is set inside. Since you intent is to remove the item from the widget it has been added, you should use the ~QTreeWidgetItem to avoid this kind of issues.

            bq. Invalid index ( 0 , 5 )

            This is clear case of using the accessing the item which does not exist in the TreeWidget

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              @ mnaydenov, QTreeWidgetItem is not a QObject

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • X Offline
                X Offline
                XGuy
                wrote on last edited by
                #7

                [quote author="mnaydenov" date="1408878175"] does the item have a parent? If yes, this will fail at some point as well.[/quote]

                This is how I make my items:
                @
                QTreeWidgetItem *sampleItem = new QTreeWidgetItem(m_ui->trvConnections->invisibleRootItem());
                m_ui->trvConnections->addTopLevelItem(sampleItem);
                @

                [quote author="mnaydenov" date="1408878175"]
                Also, as stated in the docs, it is not save to delete QObject directly because it might receive events later on; you should use deleteLater().
                [/quote]
                As SGaist stated QTreeWidgetItem is not a QObject so I can't use deleteLater().

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  XGuy
                  wrote on last edited by
                  #8

                  [quote author="Dheerendra" date="1408881394"]Also when you are using delete, it may be failing to delete the data which is set inside.[/quote]
                  well I'm afraid this is not the case because I check the memory using memory editor in Qt Creator and as Delete operator gets called the memory value is changed! so Delete is doing it's job.

                  [quote author="Dheerendra" date="1408881394"]
                  Since you intent is to remove the item from the widget it has been added, you should use the ~QTreeWidgetItem to avoid this kind of issues.[/quote]
                  So is it alright if i use ~QTreeWidgetItem() instead of delete operator?

                  [quote author="Dheerendra" date="1408881394"]
                  bq. Invalid index ( 0 , 5 )

                  This is clear case of using the accessing the item which does not exist in the TreeWidget[/quote]
                  As you can see in the code I've just posted above I'm getting the index from an Item which "itemChanged" signal provided me, if this is the case then that sounds to a bug inside Qt which I'm very skeptical of!

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andreyc
                    wrote on last edited by
                    #9

                    [quote author="XGuy" date="1408940403"]So is it alright if i use ~QTreeWidgetItem() instead of delete operator?[/quote]

                    Without delete you will loose memory on each rename. It is not big amount because ~QTreeWidgetItem() deletes its internals. You can verify it my watching an allocated memory for your app and do many renames.

                    Have you checked a backtrace from a segfault?
                    What does it show?

                    1 Reply Last reply
                    0
                    • X Offline
                      X Offline
                      XGuy
                      wrote on last edited by
                      #10

                      [quote author="andreyc" date="1408943914"][quote author="XGuy" date="1408940403"]So is it alright if i use ~QTreeWidgetItem() instead of delete operator?[/quote]

                      Have you checked a backtrace from a segfault?
                      What does it show?
                      [/quote]

                      Here is the stack trace as requested and bare in mind that sometimes this segFault leads to Qt Creator crash itself!!
                      Number 55 i mean this line("55 main main.cpp 197 0×30d59f ") points to main.cpp and it reads: return app.exec();

                      0 QObject::destroyed Qt5Cored 0x5b04f441
                      1 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648fa5d0
                      2 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648f15f0
                      3 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648f17cb
                      4 QObject::destroyed Qt5Cored 0x5b2785df
                      5 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648f5a58
                      6 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648f7857
                      7 QObject::destroyed Qt5Cored 0x5b33b210
                      8 QObject::destroyed Qt5Cored 0x5b33ac41
                      9 QObject::destroyed Qt5Cored 0x5b3f24f6
                      10 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648f2cca
                      11 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648f44c8
                      12 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x648f19c1
                      13 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x649132b9
                      14 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x6486f116
                      15 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x64873c29
                      16 QObject::destroyed Qt5Cored 0x5b33b210
                      17 QObject::destroyed Qt5Cored 0x5b33ac41
                      18 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x64a1e16b
                      19 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x64913827
                      20 QObject::destroyed Qt5Cored 0x5b2ecaa3
                      21 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x64582322
                      22 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x64581ec5
                      23 QObject::destroyed Qt5Cored 0x5b2ec814
                      24 QObject::destroyed Qt5Cored 0x5b2f20c9
                      25 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x6457d777
                      26 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x645c6869
                      27 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x64582472
                      28 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x6458005b
                      29 QObject::destroyed Qt5Cored 0x5b2ec814
                      30 QObject::destroyed Qt5Cored 0x5b3fb938
                      31 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x6457f455
                      32 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x645fe34a
                      33 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x645fd5d1
                      34 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x6458233e
                      35 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x6457fe54
                      36 QObject::destroyed Qt5Cored 0x5b2ec814
                      37 QObject::destroyed Qt5Cored 0x5b3fb938
                      38 QInputMethod::localeChanged Qt5Guid 0x65bf6fd4
                      39 QInputMethod::localeChanged Qt5Guid 0x65bf6809
                      40 QInputMethod::localeChanged Qt5Guid 0x65bded21
                      41 QInputMethod::localeChanged Qt5Guid 0x65bdeca9
                      42 QWindowsGuiEventDispatcher::sendPostedEvents qwindowsguieventdispatcher.cpp 86 0x627edce4
                      43 QObject::destroyed Qt5Cored 0x5b374a67
                      44 gapfnScSendMessage USER32 0x75fb62fa
                      45 GetThreadDesktop USER32 0x75fb6d3a
                      46 CharPrevW USER32 0x75fb77c4
                      47 DispatchMessageW USER32 0x75fb788a
                      48 QObject::destroyed Qt5Cored 0x5b375b53
                      49 QWindowsGuiEventDispatcher::processEvents qwindowsguieventdispatcher.cpp 78 0x627edc4e
                      50 QObject::destroyed Qt5Cored 0x5b2e93ec
                      51 QObject::destroyed Qt5Cored 0x5b2e9552
                      52 QObject::destroyed Qt5Cored 0x5b2ecd6d
                      53 QInputMethod::localeChanged Qt5Guid 0x65bf6598
                      54 QAbstractScrollArea::setHorizontalScrollBarPolicy Qt5Widgetsd 0x6457f9a9
                      55 main main.cpp 197 0x30d59f
                      56 WinMain qtmain_win.cpp 131 0x47831a
                      57 __tmainCRTStartup crtexe.c 547 0x4761d0
                      58 WinMainCRTStartup crtexe.c 371 0x475f5f
                      59 BaseThreadInitThunk kernel32 0x7646338a
                      60 RtlInitializeExceptionChain ntdll32 0x773d9f72
                      61 RtlInitializeExceptionChain ntdll32 0x773d9f45

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andreyc
                        wrote on last edited by
                        #11

                        I see multiple QObject::destroyed for the same address
                        @
                        7 QObject::destroyed Qt5Cored 0×5b33b210
                        16 QObject::destroyed Qt5Cored 0×5b33b210
                        29 QObject::destroyed Qt5Cored 0×5b2ec814
                        36 QObject::destroyed Qt5Cored 0×5b2ec814
                        @
                        Does it mean that the same object is destroyed or it is an address of the function?
                        The fact that it crashes QtCreator tells that there is something wrong in the memory handling.

                        I created simple test with delete item;. It removes the entry from QTReeWidget without any crashes.

                        1 Reply Last reply
                        0
                        • X Offline
                          X Offline
                          XGuy
                          wrote on last edited by
                          #12

                          [quote author="andreyc" date="1408995396"]I see multiple QObject::destroyed for the same address
                          @
                          7 QObject::destroyed Qt5Cored 0×5b33b210
                          16 QObject::destroyed Qt5Cored 0×5b33b210
                          29 QObject::destroyed Qt5Cored 0×5b2ec814
                          36 QObject::destroyed Qt5Cored 0×5b2ec814
                          @
                          Does it mean that the same object is destroyed or it is an address of the function?
                          [/quote]
                          The reason for this is that i have sub-classed QtreeWidgetItem and every item holds a button and a checkbox and another pointer to a class. I think these destroy objects simply refer to this.
                          Anyway I'm sure there is sth wrong with my code and this is no Qt's problem, and Indeed i have changed my code so that it is working. now the item rename takes place in another form.
                          So i would rather thank u all for being kind and responding to my problem. i think it's high time to mark this thread as solved.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved