Unsolved Release memory from QAbstractItemModel
-
Hi all,
I am using QAbstractItemModel to maintain a list in qt application. If I use
foreach (QObject *obj, _items) {
obj->deleteLater();
}
_items.clear();whether it will release the memory ? Because in case std::vector memory remains attached to the vector after vector clear. Is it same with QAbstractItemModel?
-
Hi,
What is _items ?
How did you create it ?You know that even if you release memory it might not be immediately claimed by the OS ?
-
@manny_lp Use qDeleteAll():
beginResetModel(); qDeleteAll(_items.begin(), _items.end()); _items.clear(); endResetModel();
-
@eyllanesc said in Release memory from QAbstractItemModel:
qDeleteAll(_items.begin(), _items.end());
This might work too:
qDeleteAll(_items);
@manny_lp
Avoid using underscores:
https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html -
@SGaist _items is list of qobjects. i.e QList<QObject *> _items;
#include "listmodel.h" #include "qdebug.h" using namespace models; ListModel::ListModel(QObject *parent): QAbstractListModel(parent) { } int ListModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : _items.size(); } QVariant ListModel::data(const QModelIndex &index, int role) const { if ((index.row() >= 0 || index.row() < _items.count()) && role == ItemRole) { if(index.isValid()) return QVariant::fromValue(_items[index.row()]); } return QVariant(); } // cppcheck-suppress unusedFunction QHash<int, QByteArray> ListModel::roleNames() const { QHash<int, QByteArray> roles; roles[ItemRole] = "item"; return roles; } void ListModel::clearItems() { foreach (QObject *obj, _items) { obj->deleteLater(); } _items.clear(); } #ifndef LISTMODEL_H #define LISTMODEL_H #include <QAbstractListModel> namespace models { /** * @brief The ListModel class * Inherited from QAbstractListModel * Can be used by all listmodel implementations */ class ListModel : public QAbstractListModel { public: // User defined roles enum Roles { ItemRole = Qt::UserRole + 1 }; explicit ListModel(QObject *parent = nullptr); /** * @brief rowCount - Returns the number of rows under the given parent. * @param parent - QModelIndex * @return - int */ virtual int rowCount(const QModelIndex &parent) const; /** * @brief data - Returns the data stored under the given role for the item referred to by the index. * @param index - index to refer. Type: QModelIndex * @param role - Role to access. Type: int/enum * @return - QVariant */ virtual QVariant data(const QModelIndex &index, int role) const; /** * @brief roleNames - Returns the model's role names. * @return - QHash<int, QByteArray> */ virtual QHash<int, QByteArray> roleNames() const; /** * @brief items - return a ref to list of items * @return - QList<QObject *> */ const QList<QObject *> &items() const { return _items; } /** * @brief size - returns the size of the list * @return - int */ int size() const { return _items.size(); } protected: // Container to hold the data QList<QObject *> _items; QList<QObject *> _emptyListToSwap; /** * @brief clearItems - Clear all the items in the list */ void clearItems(); }; } #endif // LISTMODEL_H
qDeleteAll also does not help.
-
@manny_lp said in Release memory from QAbstractItemModel:
qDeleteAll also does not help.
? What exactly is your issue? (Apart from the fact that you don't seem to actually call
ListModel::clearItems()
)?