[Interest] Managing memory with QObject*-models exposed to QML

Ola Røer Thorsen ola at silentwings.no
Thu Jul 14 11:02:30 CEST 2016


I've found that creating C++ models containing a list of QObject* based on
QAbstractListModels and using them in Quick views such as ListView is very
useful. I'm not the only one, there are several implementations of
"QObjectListModel" around, and I've got my own as well.

On quite rare ocations I have had crashes deep inside the QML engine
itself, that are very hard to reproduce, when rapidly adding and removing +
deleting objects in the model from C++. I think this probably has to do
with the Quick view's delegate that might still be using an Object that was
just deleted.

I've made sure that all objects put into the list model are created in C++
and they do have a parent to avoid qml taking ownership of them. When
removing from a model, they are first removed  (beginRemoveRows, remove
from array, endRemoveRows), then finally deleted using
object->deleteLater().

My worry here is that deleteLater() is sometimes still too soon for the QML
engine to keep up, especially when there is a lot of things going on in the
Quick scene.

Could this be the case? If so, what is the rock-solid way to do this?

One ugly "fix" that I would like to avoid is to put the objects in some
queue and delete them after a given "safe time duration", like
"deleteMuchLater()".

Another way idea that I have not tried is to use QSharedPointer, and expose
some wrapper Q_GADGET-based type to QML that contains this pointer with
some raw pointer property, but this introduces shared pointers to my C++
application and changes a lot of other things.

I'd appreciate any input on if this is a real problem to begin with, and if
so, how to work around it.

Thanks,
Ola
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160714/a7459442/attachment.html>


More information about the Interest mailing list