W
W
WTFAYD2018-04-17 10:29:42
Qt
WTFAYD, 2018-04-17 10:29:42

Deleting an element from a Qt container causes the dynamically created object to be deleted?

I will show everything with an example.

QMap<int, PlotCurve*> m_curveMap;
// ...
PlotCurve* curve = new PlotCurve(this, data);
m_curveMap[id] = curve;
// ...
m_curveMap.remove(id);

Will the element under the id key be removed from the heap in this case, or is it only removed from the container? If not, what is the best way to implement deletion from memory and container at the same time?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel K, 2018-05-02
@PavelK

Poznovato, but still - no, it does not delete the object, there remains a broken pointer. If you need to clear it completely, then qDeleteAll(); map.clear(); And it is better to use QSharedPointer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question