QCustomPlot Discussion and Comments

plotLayout()->clear() on Windows crashesReturn to overview

Hi there,

I'm playing with multiple plots in a grid (following tutorial here http://www.qcustomplot.com/documentation/thelayoutsystem.html), but I can't seem to clear the default AxisRect to begin with. It works fine on Linux and Mac, but crashes on Windows. Also, no luck using plotLayout()->remove(plotLayout->element(0,0)), which also crashes. As a definite, simple example:

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->widget->plotLayout()->clear();
}

MainWindow::~MainWindow()
{
    delete ui;
}


mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QCustomPlot" name="widget" native="true">
    <property name="geometry">
     <rect>
      <x>9</x>
      <y>9</y>
      <width>371</width>
      <height>221</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>QCustomPlot</class>
   <extends>QWidget</extends>
   <header>qcustomplot.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Anyone come across this before?

Update:

It seems you can call plotLayout()->take(element), but if you use plotLayout()->takeAt(index)->deleteLater() it will crash. So visually it works, but this generates a memory leak.

Very strange. You can follow the code path from creation of the layout element up to the takeAt call, and you'll see that nothing special is happening. Are you certain you have an intact build of Qt on your system? Have you tried this with a different Qt version, preferentiably a pre-built one from the Qt website?

I'd suspect there's no problem in QCP code at those locations, these cases are tested at every release for problems via valgrind's memory profiler and different static analyzers, and nothing has shown up. It's also the first time an issue with clear/takeAt was reported.

I once had a discussion with a user which (so we concluded) had a broken Qt binary which caused QPointer to not behave as specified in his applications (If I remember corretly it acted more like a QSharedPointer, deleting the contents when it gets cleared -- even though QPointer is clearly not supposed to do that). That also caused alot of confusion, so I'm wary of broken Qt implementations/compiles.

It's with Qt 5.4 directly from the website. I'll try with a fresh install of 5.5 and report back...

I can confirm that this is fixed with the new version 2.0.0beta.

Can you confirm that this issuse is fixed?
I’m encountering the same issuse with a newer version.