QCustomPlot Discussion and Comments

【BUG】QColorMap delete data twiceReturn to overview

When two colorMap use the same data and copy is false, the data will be deleted twice.

QCPColorMap::~QCPColorMap()
{
  delete mMapData;
}

void QCPColorMap::setData(QCPColorMapData *data, bool copy)
{
  if (mMapData == data)
  {
    qDebug() << Q_FUNC_INFO << "The data pointer is already in (and owned by) this plottable" << reinterpret_cast<quintptr>(data);
    return;
  }
  if (copy)
  {
    *mMapData = *data;
  } else
  {
    delete mMapData;
    mMapData = data;
  }
  mMapImageInvalidated = true;
}

Hi lancelot, thanks for the feedback.

Note that the QCPColorMap::setData method has the following documentation:

If copy is set to true, the data object will only be copied. if false, the color map
takes ownership of the passed data
and replaces the internal data pointer with it. This is
significantly faster than copying for large datasets.

Taking ownership means that you must not pass that pointer around to other instances that also (think they) take ownership. Ownership is precisely the right and obligation to do memory management.

However, I agree with you that this should be written more clearly in the documentation because most other plottables do support data sharing via shared pointers. In fact, I will look into setting up the same shared pointer architecture for QCPColorMap (I don't remember at the moment why I refrained from doing that when the other plottables got their update).

sorry, I did not pay attention to the documentation.
I just wonder why QColorMap does not use data sharing