use QSharedPointer in DeckListModel

This commit is contained in:
RickyRister 2025-12-20 21:47:08 -08:00
parent d4bbb6f881
commit 8cc7a61000
2 changed files with 14 additions and 11 deletions

View file

@ -5,14 +5,15 @@
DeckListModel::DeckListModel(QObject *parent)
: QAbstractItemModel(parent), lastKnownColumn(1), lastKnownOrder(Qt::AscendingOrder)
{
// This class will leak the decklist object. We cannot safely delete it in the dtor because the deckList field is a
// non-owning pointer and another deckList might have been assigned to it.
// `DeckListModel::cleanList` also leaks for the same reason.
// TODO: fix the leak
deckList = new DeckList;
deckList = QSharedPointer<DeckList>(new DeckList());
root = new InnerDecklistNode;
}
DeckListModel::DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList) : DeckListModel(parent)
{
setDeckList(deckList);
}
DeckListModel::~DeckListModel()
{
delete root;
@ -580,13 +581,13 @@ void DeckListModel::setActiveFormat(const QString &_format)
void DeckListModel::cleanList()
{
setDeckList(new DeckList);
setDeckList(QSharedPointer<DeckList>(new DeckList()));
}
/**
* @param _deck The deck.
*/
void DeckListModel::setDeckList(DeckList *_deck)
void DeckListModel::setDeckList(const QSharedPointer<DeckList> &_deck)
{
if (deckList != _deck) {
deckList = _deck;

View file

@ -240,6 +240,7 @@ signals:
public:
explicit DeckListModel(QObject *parent = nullptr);
explicit DeckListModel(QObject *parent, const QSharedPointer<DeckList> &deckList);
~DeckListModel() override;
/**
@ -323,11 +324,12 @@ public:
* @brief Removes all cards and resets the model.
*/
void cleanList();
[[nodiscard]] DeckList *getDeckList() const
[[nodiscard]] QSharedPointer<DeckList> getDeckList() const
{
return deckList;
}
void setDeckList(DeckList *_deck);
void setDeckList(const QSharedPointer<DeckList> &_deck);
/**
* @brief Apply a function to every card in the deck tree.
@ -370,7 +372,7 @@ public:
void setActiveGroupCriteria(DeckListModelGroupCriteria::Type newCriteria);
private:
DeckList *deckList; /**< Pointer to the deck loader providing the underlying data. */
QSharedPointer<DeckList> deckList; /**< Pointer to the decklist providing the underlying data. */
InnerDecklistNode *root; /**< Root node of the model tree. */
DeckListModelGroupCriteria::Type activeGroupCriteria = DeckListModelGroupCriteria::MAIN_TYPE;
int lastKnownColumn; /**< Last column used for sorting. */