Merge pull request #1239 from nextcloud/fix/device-memory-leak

Fix memory leak with device pointer
This commit is contained in:
Camila Ayres 2019-05-08 20:14:49 +02:00 committed by GitHub
commit 1f1a7a27f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 11 deletions

View file

@ -92,19 +92,19 @@ private:
public:
// Takes ownership of the device
explicit PUTFileJob(AccountPtr account, const QString &path, QIODevice *device,
explicit PUTFileJob(AccountPtr account, const QString &path, std::unique_ptr<QIODevice> device,
const QMap<QByteArray, QByteArray> &headers, int chunk, QObject *parent = nullptr)
: AbstractNetworkJob(account, path, parent)
, _device(device)
, _device(device.release())
, _headers(headers)
, _chunk(chunk)
{
_device->setParent(this);
}
explicit PUTFileJob(AccountPtr account, const QUrl &url, QIODevice *device,
explicit PUTFileJob(AccountPtr account, const QUrl &url, std::unique_ptr<QIODevice> device,
const QMap<QByteArray, QByteArray> &headers, int chunk, QObject *parent = nullptr)
: AbstractNetworkJob(account, QString(), parent)
, _device(device)
, _device(device.release())
, _headers(headers)
, _url(url)
, _chunk(chunk)

View file

@ -305,7 +305,7 @@ void PropagateUploadFileNG::startNextChunk()
return;
}
auto device = new UploadDevice(&propagator()->_bandwidthManager);
auto device = std::make_unique<UploadDevice>(&propagator()->_bandwidthManager);
const QString fileName = _fileToUpload._path;
if (!device->prepareAndOpen(fileName, _sent, _currentChunkSize)) {
@ -328,13 +328,14 @@ void PropagateUploadFileNG::startNextChunk()
QUrl url = chunkUrl(_currentChunk);
// job takes ownership of device via a QScopedPointer. Job deletes itself when finishing
PUTFileJob *job = new PUTFileJob(propagator()->account(), url, device, headers, _currentChunk, this);
auto devicePtr = device.get(); // for connections later
PUTFileJob *job = new PUTFileJob(propagator()->account(), url, std::move(device), headers, _currentChunk, this);
_jobs.append(job);
connect(job, &PUTFileJob::finishedSignal, this, &PropagateUploadFileNG::slotPutFinished);
connect(job, &PUTFileJob::uploadProgress,
this, &PropagateUploadFileNG::slotUploadProgress);
connect(job, &PUTFileJob::uploadProgress,
device, &UploadDevice::slotJobUploadProgress);
devicePtr, &UploadDevice::slotJobUploadProgress);
connect(job, &QObject::destroyed, this, &PropagateUploadFileCommon::slotJobDestroyed);
job->start();
propagator()->_activeJobList.append(this);

View file

@ -89,7 +89,7 @@ void PropagateUploadFileV1::startNextChunk()
QString path = _fileToUpload._file;
UploadDevice *device = new UploadDevice(&propagator()->_bandwidthManager);
auto device = std::make_unique<UploadDevice>(&propagator()->_bandwidthManager);
qint64 chunkStart = 0;
qint64 currentChunkSize = fileSize;
bool isFinalChunk = false;
@ -134,16 +134,16 @@ void PropagateUploadFileV1::startNextChunk()
}
// Soft error because this is likely caused by the user modifying his files while syncing
abortWithError(SyncFileItem::SoftError, device->errorString());
delete device;
return;
}
// job takes ownership of device via a QScopedPointer. Job deletes itself when finishing
PUTFileJob *job = new PUTFileJob(propagator()->account(), propagator()->_remoteFolder + path, device, headers, _currentChunk, this);
auto devicePtr = device.get(); // for connections later
PUTFileJob *job = new PUTFileJob(propagator()->account(), propagator()->_remoteFolder + path, std::move(device), headers, _currentChunk, this);
_jobs.append(job);
connect(job, &PUTFileJob::finishedSignal, this, &PropagateUploadFileV1::slotPutFinished);
connect(job, &PUTFileJob::uploadProgress, this, &PropagateUploadFileV1::slotUploadProgress);
connect(job, &PUTFileJob::uploadProgress, device, &UploadDevice::slotJobUploadProgress);
connect(job, &PUTFileJob::uploadProgress, devicePtr, &UploadDevice::slotJobUploadProgress);
connect(job, &QObject::destroyed, this, &PropagateUploadFileCommon::slotJobDestroyed);
if (isFinalChunk)
adjustLastJobTimeout(job, fileSize);