Ensure database contents are released right away.

When we lock a database, we reset the database pointer to
free its resources. Since various other widgets besides the
DatabaseWidget hold references to the shared pointer object,
however, it cannot be guaranteed that the actual database
object will be freed right away. This patch adds a releaseData()
method which is called upon database lock to ensure all
residual data is cleared without having to rely on the actual
database object being cleaned up.
This commit is contained in:
Janek Bevendorff 2019-11-08 13:34:40 +01:00
parent 87ca7c7f7b
commit 22af66e3b5
3 changed files with 43 additions and 8 deletions

View File

@ -72,11 +72,7 @@ Database::Database(const QString& filePath)
Database::~Database()
{
s_uuidMap.remove(m_uuid);
if (m_modified) {
emit databaseDiscarded();
}
releaseData();
}
QUuid Database::uuid() const
@ -378,6 +374,42 @@ bool Database::import(const QString& xmlExportPath, QString* error)
return true;
}
/**
* Release all stored group, entry, and meta data of this database.
*
* Call this method to ensure all data is cleared even if valid
* pointers to this Database object are still being held.
*
* A previously reparented root group will not be freed.
*/
void Database::releaseData()
{
s_uuidMap.remove(m_uuid);
m_uuid = QUuid();
if (m_modified) {
emit databaseDiscarded();
}
m_data = DatabaseData();
if (m_rootGroup && m_rootGroup->parent() == this) {
delete m_rootGroup;
}
if (m_metadata) {
delete m_metadata;
}
if (m_fileWatcher) {
delete m_fileWatcher;
}
m_deletedObjects.clear();
m_commonUsernames.clear();
m_initialized = false;
m_modified = false;
}
/**
* Remove the old backup and replace it with a new one
* backups are named <filename>.old.<extension>

View File

@ -29,7 +29,6 @@
#include "crypto/kdf/Kdf.h"
#include "format/KeePass2.h"
#include "keys/CompositeKey.h"
class Entry;
enum class EntryReferenceType;
class FileWatcher;
@ -76,6 +75,8 @@ public:
bool extract(QByteArray&, QString* error = nullptr);
bool import(const QString& xmlExportPath, QString* error = nullptr);
void releaseData();
bool isInitialized() const;
void setInitialized(bool initialized);
bool isModified() const;
@ -182,9 +183,9 @@ private:
bool restoreDatabase(const QString& filePath);
bool performSave(const QString& filePath, QString* error, bool atomic, bool backup);
Metadata* const m_metadata;
QPointer<Metadata> const m_metadata;
DatabaseData m_data;
Group* m_rootGroup;
QPointer<Group> m_rootGroup;
QList<DeletedObject> m_deletedObjects;
QPointer<QTimer> m_timer;
QPointer<FileWatcher> m_fileWatcher;

View File

@ -418,6 +418,8 @@ void DatabaseWidget::replaceDatabase(QSharedPointer<Database> db)
// Keep the instance active till the end of this function
Q_UNUSED(oldDb);
#endif
oldDb->releaseData();
}
void DatabaseWidget::cloneEntry()