Results 1 to 2 of 2

Thread: Qwt proper pointer management

  1. #1

    Default Qwt proper pointer management

    Dear community, I have started to learn cpp just a few month ago and thanks to other threads of forum, I become more familiar with Qt and Qwt as well. I want to ask you how to manage Qwt`s pointers properly to avoid memory leaks.
    Consider a snippet of code where I try manually to delete my QwtPlotCurve* pointer that makes my app crash. This might happen due to attempt of double delete because the statement “detachItems(QwtPlotItem::Rtti_PlotCurve, true)” has already deleted it. I just want to ensure that using it is valid way to avoid memory leaks. Does this statement also delete curve-associated pointers like QwtPlotMarker*, QwtText*, etc if they would exist or I need to watch them myself? Just set pointer to nullptr is enough here to dangling pointer?
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. ui->stackedWidget->setCurrentIndex(1);
    4. QwtPlot *plotWidget = findChild<QwtPlot *>("plotWidget");
    5. curve = new QwtPlotCurve(); //curve is class member declared in header
    6. qDebug() << "Curve created, its address:" << curve;
    7. curve->setPen(Qt::blue);
    8. int y;
    9. QVector<double> xData, yData; //vectors to plot curve data
    10. for (int i=-100; i<=100; i++)
    11. {
    12. y=i*qSin(i);
    13. xData.append(i); //fill vectors
    14. yData.append(y); //fill vectors
    15. }
    16. int size = xData.size();
    17. curve->setSamples(xData.data(), yData.data(), size);
    18. curve->attach(plotWidget);
    19. plotWidget->setAxisAutoScale(QwtPlot::xBottom);
    20. plotWidget->setAxisAutoScale(QwtPlot::yLeft);
    21. Replot to update the plot with the curve
    22. plotWidget->replot();
    23. }
    24. void MainWindow::on_pushButton_2_clicked()
    25. {
    26. ui->stackedWidget->setCurrentIndex(0);
    27. ui->plotWidget->detachItems(QwtPlotItem::Rtti_PlotCurve, true);
    28. //delete curve; ------ this line crashes my app if uncommented
    29. curve = nullptr;
    30. if(curve==nullptr)
    31. { qDebug()<<"curve is nullptr";}
    32. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,272
    Thanks
    308
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qwt proper pointer management

    If "curve" is derived from QObject, then it is owned by a parent QObject. When you are inside a slot, this ownership is not usually cleaned up until after the slot exits and control returns to the Qt event loop. So when you "delete" a QObject, your code can crash because the parent QObject still thinks it has control of the pointer. Do something like this:

    Qt Code:
    1. curve->deleteLater();
    2. curve = nullptr;
    To copy to clipboard, switch view to plain text mode 

    And be sure to check the value of curve elsewhere in your code to make sure you aren't trying to use a null pointer. deleteLater() tells Qt to delete the object after it has returned control to the event loop, not immediately the way "delete" does.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. File Management
    By Arshia Aghaei in forum Newbie
    Replies: 1
    Last Post: 1st July 2015, 07:29
  2. Replies: 3
    Last Post: 23rd May 2015, 14:57
  3. Replies: 1
    Last Post: 4th December 2010, 18:20
  4. Qt 4.2 Shortcut Management
    By dvmorris in forum Qt Programming
    Replies: 20
    Last Post: 5th March 2007, 21:48
  5. Memory management in QT
    By Gayathri in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2006, 08:21

Tags for this Thread

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.