Checksums: Make file ownership more explicit
authorChristian Kamm <mail@ckamm.de>
Thu, 15 Nov 2018 08:06:23 +0000 (09:06 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:24 +0000 (10:58 +0100)
src/common/checksums.cpp
src/common/checksums.h

index 3776c601fe7714fc7e301940298ee6c87be42db5..354b88f5cfe35fba517e11d20487b0ac17720990 100644 (file)
@@ -208,6 +208,10 @@ ComputeChecksum::ComputeChecksum(QObject *parent)
 {
 }
 
+ComputeChecksum::~ComputeChecksum()
+{
+}
+
 void ComputeChecksum::setChecksumType(const QByteArray &type)
 {
     _checksumType = type;
@@ -221,13 +225,13 @@ QByteArray ComputeChecksum::checksumType() const
 void ComputeChecksum::start(const QString &filePath)
 {
     qCInfo(lcChecksums) << "Computing" << checksumType() << "checksum of" << filePath << "in a thread";
-    _file = new QFile(filePath, this);
+    _file.reset(new QFile(filePath));
     if (!_file->open(QIODevice::ReadOnly)) {
         qCWarning(lcChecksums) << "Could not open file" << filePath << "for reading to compute a checksum" << _file->errorString();
         emit done(QByteArray(), QByteArray());
         return;
     }
-    start(_file);
+    start(_file.get());
 }
 
 void ComputeChecksum::start(QIODevice *device)
@@ -286,8 +290,7 @@ QByteArray ComputeChecksum::computeNow(QIODevice *device, const QByteArray &chec
 void ComputeChecksum::slotCalculationDone()
 {
     // Close the file and delete the instance
-    if (_file)
-        delete _file;
+    _file.reset(nullptr);
 
     QByteArray checksum = _watcher.future().result();
     if (!checksum.isNull()) {
index 754e1ae5a39c0186cf13306658f80d0d39da2eea..e057fb7ad1a4e2129b7094a791e21a0386a4c1ff 100644 (file)
@@ -25,6 +25,8 @@
 #include <QByteArray>
 #include <QFutureWatcher>
 
+#include <memory>
+
 class QFile;
 
 namespace OCC {
@@ -82,6 +84,7 @@ class OCSYNC_EXPORT ComputeChecksum : public QObject
     Q_OBJECT
 public:
     explicit ComputeChecksum(QObject *parent = nullptr);
+    ~ComputeChecksum();
 
     /**
      * Sets the checksum type to be used. The default is empty.
@@ -129,7 +132,7 @@ private:
     QByteArray _checksumType;
 
     // The convenience wrapper may open a file and must close it too
-    QFile *_file = nullptr;
+    std::unique_ptr<QFile> _file;
 
     // watcher for the checksum calculation thread
     QFutureWatcher<QByteArray> _watcher;