Results 1 to 6 of 6

Thread: Deleting pointer to QwtPlot segmentation fault

  1. #1
    Join Date
    Feb 2018
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Deleting pointer to QwtPlot segmentation fault

    Hello, I have become the new maintainer of an old Qt/QWT project.
    I am almost done porting it to Qt 5 from Qt 4 and from Qwt 4 to Qwt 6.

    However, i'm getting a segmentation fault when deleting the pointer to a QwtPlot.

    The code is very old and i have yet to clean it up as it is quite large and right now i'm just trying to get it to work.

    Constructor for the class "axes"
    Qt Code:
    1. Axes::Axes(const QString& title, QTabWidget* tabwidget, QMenu* menu){
    2. title_ = title;
    3. tabwidget_ = tabwidget;
    4. menu_ = menu;
    5. //QScopedPointer<QwtPlot> axes_(new QwtPlot(title_));
    6.  
    7. axes_ = new QwtPlot(title_); // This is the pointer i cannot delete
    8.  
    9. graphdialog_ = new GraphDialog(title_);
    10. xpicker_ = new QwtPicker(static_cast<QWidget*>(axes_->axisWidget(2)));
    11. ypicker_ = new QwtPicker(static_cast<QWidget*>(axes_->axisWidget(0)));
    12. canvaspicker_ = new QwtPlotPicker(axes_->canvas());
    13. axes_->insertLegend(new QwtLegend());
    14. windowaction_ = new QAction(title_, this);
    15. autoscaleaction_ = new QAction("Autoscale", this);
    16. gridaction_ = new QAction("Show grid", this);
    17. xaxislimitsaction_ = new QAction("Set x scale", this);
    18. yaxislimitsaction_ = new QAction("Set y scale", this);
    19. grid_ = new QwtPlotGrid();
    20. grid_->hide();
    21. grid_->show();
    22. tabwidget_->addTab(axes_, title_);
    23. menu_->insertAction(0,autoscaleaction_);
    24. menu_->insertAction(0,gridaction_);
    25. graphdialog_->menu()->insertAction(0,autoscaleaction_);
    26. graphdialog_->menu()->insertAction(0,gridaction_);
    27. pickermachine_ = new QwtPickerClickPointMachine();
    28. xpicker_->setStateMachine(pickermachine_);
    29. xpicker_->setTrackerMode(QwtPicker::ActiveOnly);
    30. ypicker_->setStateMachine(pickermachine_);
    31. ypicker_->setTrackerMode(QwtPicker::ActiveOnly);
    32. canvaspicker_->setStateMachine(pickermachine_);
    33. canvaspicker_->setTrackerMode(QwtPicker::ActiveOnly);
    34. graphsNum_ = 0;
    35. QFont font = axes_->axisFont(0);
    36. font.setPointSize(8);
    37. axes_->setAxisFont(0,font);
    38. axes_->setAxisFont(1,font);
    39. axes_->setAxisFont(2,font);
    40. axes_->setAxisFont(3,font);
    41. axes_->legend()->setFont(font);
    42. font = axes_->titleLabel()->font();
    43. font.setPointSize(12);
    44. axes_->titleLabel()->setFont(font);
    45. axes_->setAutoFillBackground(true);
    46. axes_->setAutoDelete(false);
    47. grid_->setMajorPen(QColor(Qt::black),0.5,Qt::DashLine);
    48. grid_->attach(axes_);
    49. autoscaleaction_->setCheckable(true);
    50. autoscaleaction_->setChecked(false);
    51. gridaction_->setCheckable(true);
    52. gridaction_->setChecked(true);
    53. windowaction_->setCheckable(true);
    54. windowaction_->setChecked(true);
    55. connect(xpicker_, SIGNAL(selected(const QPolygon &)), this, SLOT(xAxisClicked(QPolygon)));
    56. connect(ypicker_, SIGNAL(selected(const QPolygon &)), this, SLOT(yAxisClicked(QPolygon)));
    57. connect(canvaspicker_, SIGNAL(selected(const QPolygon &)), this, SLOT(canvasClicked(QPolygon)));
    58. connect(windowaction_, SIGNAL(toggled(bool)), this, SLOT(setDocked(bool)));
    59. connect(autoscaleaction_, SIGNAL(toggled(bool)), this, SLOT(setAutoscale(bool)));
    60. connect(gridaction_, SIGNAL(toggled(bool)), this, SLOT(showGrid(bool)));
    61. connect(xaxislimitsaction_, SIGNAL(triggered()), this, SLOT(xAxisClicked()));
    62. connect(yaxislimitsaction_, SIGNAL(triggered()), this, SLOT(yAxisClicked()));
    63.  
    64. axes_->axisWidget(QwtPlot::xBottom)->setMinBorderDist(0,20);
    65. axes_->plotLayout()->setAlignCanvasToScales(true);
    66.  
    67. xhistory_ = 30;
    68. xmax_ = 0;
    69. ymin_ = 1e10;
    70. ymax_ = -1e10;
    71. axes_->setContextMenuPolicy(Qt::CustomContextMenu);
    72. connect(axes_, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
    73. }
    To copy to clipboard, switch view to plain text mode 

    The destructor:

    Qt Code:
    1. Axes::~Axes(){
    2. #ifdef SHOW_DESTRUCTORS
    3. qDebug() << "Destroying " << axes_->title().text();
    4. #endif
    5.  
    6. foreach(Graph* graph, graphs_)
    7. delete graph;
    8. graphs_.clear();
    9. barplots_.clear();
    10. graphsNum_ = 0;
    11. delete graphdialog_;
    12. delete windowaction_;
    13. delete autoscaleaction_;
    14. delete gridaction_;
    15. delete axes_; // this line causes the segmentation fault
    16.  
    17. #ifdef SHOW_DESTRUCTORS
    18. qDebug() << "Destroyed!!";
    19. #endif
    20. }
    To copy to clipboard, switch view to plain text mode 

    Basically every forum I have read with "deleting pointer causes segmentation fault" is because it hasn't been assigned with "new" - but it is in my case.
    I also tried using a smart pointer but couldn't get it to work.

    I hope you can help me

    Edit: I forgot to mention, we have an old compiled version of the project, where this works - so something, either in Qt or Qwt has changed so it doesn't work anymore.

    Best Regards

    David

  2. #2
    Join Date
    Feb 2018
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Deleting pointer to QwtPlot segmentation fault

    I found a solutions, though I'm not entirely sure I understand why.

    But editing the deconstructor from :
    Qt Code:
    1. Axes::~Axes(){
    2. #ifdef SHOW_DESTRUCTORS
    3. qDebug() << "Destroying " << axes_->title().text();
    4. #endif
    5.  
    6. foreach(Graph* graph, graphs_)
    7. delete graph;
    8. graphs_.clear();
    9. barplots_.clear();
    10. graphsNum_ = 0;
    11. delete graphdialog_;
    12. delete windowaction_;
    13. delete autoscaleaction_;
    14. delete gridaction_;
    15. delete axes_; // this line causes the segmentation fault
    16.  
    17. #ifdef SHOW_DESTRUCTORS
    18. qDebug() << "Destroyed!!";
    19. #endif
    20. }
    To copy to clipboard, switch view to plain text mode 

    to

    Qt Code:
    1. Axes::~Axes(){
    2. #ifdef SHOW_DESTRUCTORS
    3. qDebug() << "Destroying " << axes_->title().text();
    4. #endif
    5.  
    6. tabwidget_->clear();
    7.  
    8. #ifdef SHOW_DESTRUCTORS
    9. qDebug() << "Destroyed!!";
    10. #endif
    11. }
    To copy to clipboard, switch view to plain text mode 

    seems to fix it and it works now.
    My guess is that tabwidget was the parent of everything and telling it to clear all works.
    I guess since the old times a lot more has been done to help automatically clear up any data on the heap/stack, such as pointers.

    I hope this can help someone else too.

    Best regards

    David

  3. #3
    Join Date
    Jul 2018
    Location
    Gouda, the Netherlands
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Deleting pointer to QwtPlot segmentation fault

    You can verify using valgrind if indeed nothing is "leaking", e.g. if indeed you don't have to free anything extra.

  4. #4
    Join Date
    Jul 2008
    Posts
    386
    Thanks
    9
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Deleting pointer to QwtPlot segmentation fault

    Hi, you probably get the segfault because you don't own "axes_" anymore. The QTabWidget takes ownership when you call addTab(), and will take care of destruction.

    Ginsengelf

  5. #5
    Join Date
    Feb 2018
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Deleting pointer to QwtPlot segmentation fault

    Quote Originally Posted by flok View Post
    You can verify using valgrind if indeed nothing is "leaking", e.g. if indeed you don't have to free anything extra.
    Thank you, I will try out Valgrind

    Best regards
    David

  6. #6
    Join Date
    Feb 2018
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Deleting pointer to QwtPlot segmentation fault

    I don't know if any of you care.
    But the project is finally succesfully ported to Qt5.10 and Qwt6.1.3
    I now have both a compiled Windows and Linux version
    It's been though, but I got through!

    Thanks to everyone who has help me out underway.

    Best regards

    David

Similar Threads

  1. Segmentation fault and lost ui pointer?
    By poporacer in forum Newbie
    Replies: 0
    Last Post: 19th December 2010, 00:42
  2. deleting invalid pointer
    By hollowhead in forum General Programming
    Replies: 11
    Last Post: 30th April 2010, 11:47
  3. Replies: 15
    Last Post: 26th October 2009, 19:47
  4. Segmentation Fault
    By Krish_ng in forum Qt Programming
    Replies: 8
    Last Post: 7th August 2007, 11:49
  5. segmentation fault
    By shamik in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 08:33

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.