From bb18358be123bd8b77fa556c5f31f04ddbdb03d6 Mon Sep 17 00:00:00 2001 From: MrStevns Date: Sun, 24 Nov 2024 14:08:31 +0100 Subject: [PATCH] Add missing QFile::close where a file has been opened. And move some closer to the open call to make sure it happens when the scope ends. --- core_lib/src/graphics/vector/vectorimage.cpp | 6 ++++++ core_lib/src/soundplayer.cpp | 4 ++++ core_lib/src/structure/filemanager.cpp | 8 +++++++- core_lib/src/structure/object.cpp | 8 ++++++-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/core_lib/src/graphics/vector/vectorimage.cpp b/core_lib/src/graphics/vector/vectorimage.cpp index 54a0b90fb..80999e975 100644 --- a/core_lib/src/graphics/vector/vectorimage.cpp +++ b/core_lib/src/graphics/vector/vectorimage.cpp @@ -82,6 +82,9 @@ bool VectorImage::read(QString filePath) { return false; } + ScopeGuard fileScope([&] { + file.close(); + }); QDomDocument doc; if (!doc.setContent(&file)) return false; // this is not a XML file @@ -123,6 +126,9 @@ Status VectorImage::write(QString filePath, QString format) debugInfo << ("file.error() = " + file.errorString()); return Status(Status::FAIL, debugInfo); } + ScopeGuard fileScope([&] { + file.close(); + }); if (format != "VEC") { diff --git a/core_lib/src/soundplayer.cpp b/core_lib/src/soundplayer.cpp index 6120b67b0..2c9ac8dd3 100644 --- a/core_lib/src/soundplayer.cpp +++ b/core_lib/src/soundplayer.cpp @@ -41,6 +41,10 @@ void SoundPlayer::init(SoundClip* clip) QFile file(clip->fileName()); file.open(QIODevice::ReadOnly); + ScopeGuard fileScope([&] { + file.close(); + }); + mBuffer.setData(file.readAll()); mBuffer.open(QBuffer::ReadOnly); diff --git a/core_lib/src/structure/filemanager.cpp b/core_lib/src/structure/filemanager.cpp index 81c878d57..6ca13de3e 100644 --- a/core_lib/src/structure/filemanager.cpp +++ b/core_lib/src/structure/filemanager.cpp @@ -114,6 +114,10 @@ Object* FileManager::load(const QString& sFileName) handleOpenProjectError(Status::ERROR_FILE_CANNOT_OPEN, dd); return nullptr; } + ScopeGuard fileScope([&] { + file.close(); + }); + dd << "Main XML exists: Yes"; QDomDocument xmlDoc; @@ -933,6 +937,9 @@ Status FileManager::rebuildMainXML(Object* object) { return Status::ERROR_FILE_CANNOT_OPEN; } + ScopeGuard fileScope([&] { + file.close(); + }); QDomDocument xmlDoc("PencilDocument"); QDomElement root = xmlDoc.createElement("document"); @@ -957,7 +964,6 @@ Status FileManager::rebuildMainXML(Object* object) QTextStream fout(&file); xmlDoc.save(fout, 2); fout.flush(); - file.close(); return Status::OK; } diff --git a/core_lib/src/structure/object.cpp b/core_lib/src/structure/object.cpp index bb25cdb1e..053f78dac 100644 --- a/core_lib/src/structure/object.cpp +++ b/core_lib/src/structure/object.cpp @@ -519,13 +519,15 @@ bool Object::exportPalette(const QString& filePath) const qDebug("Error: cannot export palette"); return false; } + ScopeGuard fileScope([&] { + file.close(); + }); if (file.fileName().endsWith(".gpl", Qt::CaseInsensitive)) exportPaletteGPL(file); else exportPalettePencil(file); - file.close(); return true; } @@ -665,6 +667,9 @@ bool Object::importPalette(const QString& filePath) { return false; } + ScopeGuard fileScope([&] { + file.close(); + }); if (file.fileName().endsWith(".gpl", Qt::CaseInsensitive)) { @@ -672,7 +677,6 @@ bool Object::importPalette(const QString& filePath) } else { importPalettePencil(file); } - file.close(); return true; }