Results 1 to 7 of 7

Thread: QPointer and double deletion

  1. #1
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QPointer and double deletion

    I have a
    Qt Code:
    1. QList<QPointer<MTR::RowBox> > rowBoxes;
    To copy to clipboard, switch view to plain text mode 
    that is a private member of my main widget.
    I want to store pointers to a few widgets in this list as well as I want to place these widgets in a gridLayout. My concern is connected to possible double deletion of these widgets as first may be done by a parent widget (received by layout) and second may be done by QList.

    I receive SIGSEV for this code:
    Qt Code:
    1. QGridLayout *qgl = new QGridLayout();
    2. pawnBox = new MTR::PawnBox();
    3.  
    4. QPointer<MTR::RowBox> rbOne = new MTR::RowBox();
    5. rowBoxes.push_back(rbOne);
    6. QPointer<MTR::RowBox> rb2 = new MTR::RowBox();
    7. rowBoxes.push_back(rb2);
    8. QPointer<MTR::RowBox> rb3 = new MTR::RowBox();
    9. rowBoxes.push_back(rb3);
    10.  
    11. QWidget *w1 = new QWidget();
    12. w1->setFixedSize(50,50);
    13. qgl->addWidget(w1,0,0,1,2);
    14. qgl->addWidget(rbOne,1,0);
    15. qgl->addWidget(pawnBox,1,1,3,1);
    16. qgl->addWidget(rb2,2,0);
    17. qgl->addWidget(rb3,3,0);
    18. gameScreen->setLayout(qgl);
    To copy to clipboard, switch view to plain text mode 

    where MTR::RowBox is a pure subclass of a QWidget.

    Backtrace is obvious:
    Qt Code:
    1. #0 0x080559d6 in QList<QPointer<MTR::RowBox> >::~QList ()
    2. #1 0x08052eb7 in MyWidget::~MyWidget ()
    3. #2 0x0804e89e in main ()
    To copy to clipboard, switch view to plain text mode 

    A quotation from QPointer class documentation:
    Guarded pointers are useful whenever you need to store a pointer to a QObject that is owned by someone else, and therefore might be destroyed while you still hold a reference to it.
    Last edited by mtrpoland; 28th September 2007 at 11:02.

  2. #2
    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: QPointer and double deletion

    Did you reimplement the destructor? And why do you use QPointer here in the first place? Based on the code you pasted, you don't need it.

  3. #3
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer and double deletion

    Did you reimplement the destructor?
    Qt Code:
    1. MyWidget::~MyWidget() {
    2. QVariant var(packRecords(rekordyLokalne));
    3. settings->setValue("rekordyLokalne",var);
    4. settings->sync();
    5. file.close();
    6. delete settings;
    7. }
    To copy to clipboard, switch view to plain text mode 

    And why do you use QPointer here in the first place?
    What do you mean by "here in the first place"?

  4. #4
    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: QPointer and double deletion

    It's an English idiom (something like "from the beginning", "upfront"). Why do you use QPointer?

  5. #5
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer and double deletion

    As I believe that when my MTR::RowBox'es get deleted the QPointers that are kept in the QList will be set to 0. Thus I hope that QList destructor takes it into account.

    Anyway I want to keep pointers (no matter whether smart or normal) to MTR::RowBox'es in a QList and use "for" loop in order to place them in a QGridLayout.
    But here comes the issue of automatical deletion of child widgets. I understand that after my actions described above each MTR::RowBox object is a child of QList and MyWidget.

    How to do it safely?

  6. #6
    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: QPointer and double deletion

    But QList destructor doesn't delete pointers it holds. It only deletes objects it holds (QPointer in this case) but QPointer destructor also doesn't delete pointers it holds.

    Child widgets get deleted automatically by the parent widget when the latter is destroyed.

  7. #7
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPointer and double deletion

    I have made some changes:
    Qt Code:
    1. QList<MTR::RowBox *> rowBoxes;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QGridLayout *qgl = new QGridLayout();
    2. pawnBox = new MTR::PawnBox();
    3. for(int i=0;i<3;++i) rowBoxes.push_back(new MTR::RowBox());
    4. qgl->addWidget(pawnBox,0,1,3,1);
    5. for(int i=0;i<3;++i) qgl->addWidget(rowBoxes[i],i,0,1,1);
    6. gameScreen->setLayout(qgl);
    To copy to clipboard, switch view to plain text mode 

    SIGSEV
    Backtrace:
    Qt Code:
    1. #0 0x08055806 in QList<MTR::RowBox*>::~QList ()
    2. #1 0x08051b77 in MyWidget::~MyWidget ()
    3. #2 0x0804e7ee in main ()
    To copy to clipboard, switch view to plain text mode 

    Could you tell me how does it happen if QList does not destroy the elements of its pointers and how to circumvent it?
    Last edited by mtrpoland; 28th September 2007 at 13: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.