Prevent memory leak in StackSupport

This commit is contained in:
Martchus 2018-06-10 22:51:43 +02:00
parent 2421cc70a8
commit 2496c495c7
3 changed files with 15 additions and 10 deletions

View File

@ -5,6 +5,8 @@
#include <QUndoStack> #include <QUndoStack>
#include <memory>
namespace QtGui { namespace QtGui {
class StackAbsorper; class StackAbsorper;
@ -17,7 +19,7 @@ public:
protected: protected:
QUndoStack *undoStack(); QUndoStack *undoStack();
bool push(CustomUndoCommand *command); bool push(std::unique_ptr<CustomUndoCommand> command);
void clearUndoStack(); void clearUndoStack();
private: private:
@ -35,7 +37,7 @@ inline QUndoStack *StackSupport::undoStack()
/*! /*!
* \brief Pushes the specified custom undo \a command to the undo stack and returns whether the redo action was successful. * \brief Pushes the specified custom undo \a command to the undo stack and returns whether the redo action was successful.
*/ */
inline bool StackSupport::push(CustomUndoCommand *command) inline bool StackSupport::push(std::unique_ptr<CustomUndoCommand> command)
{ {
if (!m_undoStack) { if (!m_undoStack) {
return false; return false;
@ -43,7 +45,7 @@ inline bool StackSupport::push(CustomUndoCommand *command)
if (command->isNoop()) { if (command->isNoop()) {
return true; // doing nothing can never fail return true; // doing nothing can never fail
} }
m_undoStack->push(command); m_undoStack->push(command.release());
return command->redoResult(); return command->redoResult();
} }

View File

@ -13,6 +13,7 @@
#include <QIcon> #include <QIcon>
#include <QMimeData> #include <QMimeData>
#include <memory>
#include <sstream> #include <sstream>
using namespace std; using namespace std;
@ -266,7 +267,7 @@ bool EntryModel::setData(const QModelIndex &index, const QVariant &value, int ro
{ {
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
if (undoStack()) { if (undoStack()) {
return push(new EntryModelSetValueCommand(this, index, value, role)); return push(make_unique<EntryModelSetValueCommand>(this, index, value, role));
} }
#endif #endif
if (!index.isValid()) { if (!index.isValid()) {
@ -388,7 +389,7 @@ bool EntryModel::insertRows(int row, int count, const QModelIndex &parent)
{ {
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
if (undoStack()) { if (undoStack()) {
return push(new EntryModelInsertRowsCommand(this, row, count, parent)); return push(make_unique<EntryModelInsertRowsCommand>(this, row, count, parent));
} }
#endif #endif
if (!parent.isValid()) { if (!parent.isValid()) {
@ -421,7 +422,7 @@ bool EntryModel::removeRows(int row, int count, const QModelIndex &parent)
{ {
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
if (undoStack()) { if (undoStack()) {
return push(new EntryModelRemoveRowsCommand(this, row, count, parent)); return push(make_unique<EntryModelRemoveRowsCommand>(this, row, count, parent));
} }
#endif #endif
if (!parent.isValid() || count <= 0) { if (!parent.isValid() || count <= 0) {
@ -441,7 +442,7 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
{ {
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
if (undoStack()) { if (undoStack()) {
return push(new EntryModelMoveRowsCommand(this, sourceParent, sourceRow, count, destinationParent, destinationChild)); return push(make_unique<EntryModelMoveRowsCommand>(this, sourceParent, sourceRow, count, destinationParent, destinationChild));
} }
#endif #endif
// check validation of specified arguments // check validation of specified arguments

View File

@ -9,6 +9,8 @@
#include <QMimeData> #include <QMimeData>
#include <QStringList> #include <QStringList>
#include <memory>
using namespace std; using namespace std;
using namespace Io; using namespace Io;
@ -153,7 +155,7 @@ bool FieldModel::setData(const QModelIndex &index, const QVariant &value, int ro
{ {
#if PASSWORD_MANAGER_GUI_QTWIDGETS #if PASSWORD_MANAGER_GUI_QTWIDGETS
if (undoStack()) { if (undoStack()) {
return push(new FieldModelSetValueCommand(this, index, value, role)); return push(make_unique<FieldModelSetValueCommand>(this, index, value, role));
} }
#endif #endif
if (!index.isValid() || !m_fields || index.row() < 0) { if (!index.isValid() || !m_fields || index.row() < 0) {
@ -294,7 +296,7 @@ bool FieldModel::insertRows(int row, int count, const QModelIndex &parent)
{ {
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
if (undoStack()) { if (undoStack()) {
return push(new FieldModelInsertRowsCommand(this, row, count)); return push(make_unique<FieldModelInsertRowsCommand>(this, row, count));
} }
#endif #endif
if (parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row) > m_fields->size()) { if (parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row) > m_fields->size()) {
@ -310,7 +312,7 @@ bool FieldModel::removeRows(int row, int count, const QModelIndex &parent)
{ {
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
if (undoStack()) { if (undoStack()) {
return push(new FieldModelRemoveRowsCommand(this, row, count)); return push(make_unique<FieldModelRemoveRowsCommand>(this, row, count));
} }
#endif #endif
if (parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) { if (parent.isValid() || row < 0 || count <= 0 || static_cast<size_t>(row + count) > m_fields->size()) {