Releasing in QObjectList-based model
-
Hello,
In document "Using C++ Models with Qt Quick Views" I've found description of an QObjectList-based model, which mostly looks cool for easy usage when you need to provide some data into QML, except one thing that I cannot really understand: how such a list is supposed to be released? There is a reference to the example, where QObject's are created without any parent explicitly specified:
@
QList<QObject*> dataList;
dataList.append(new DataObject("Item 1", "red"));
dataList.append(new DataObject("Item 2", "green"));
dataList.append(new DataObject("Item 3", "blue"));
dataList.append(new DataObject("Item 4", "yellow"));
@And also there is no any explicit destruction of the objects in this list. I've checked the whole example and put debug messages into DataObject destructor - it does not called at all.
So my question is: Is Qt Quick engine responsible for such a model releasing (by deleting of each object in a list), or it is developer responsibility to make sure that everything is properly released? Can it be that example code contains mistake that is widely reproduced by other developers in their projects?
Thanks for answers.
-
Unless you assign a parent, you are responsible for deleting the object.
-
Yeah, but sometimes, Qt library uses re-parenting, like here:
@QNetworkAccessManager::setCookieJar(QNetworkCookieJar * cookieJar)@
bq. Note: QNetworkAccessManager takes ownership of the cookieJar object.
Well.. in case of QObjectList-based model there is no any notes like that, but in example there is no any parenting for item objects.
-
If the objects are needed for the whole duration of the execution, then no parent is strictly needed: the memory will be cleaned by the operating system after you close the application.
It would be much better to provide an example that does set a parent, you are right. You can file a bug report.