Skip to content

Commit

Permalink
Add missing QFile::close where a file has been opened.
Browse files Browse the repository at this point in the history
And move some closer to the open call to make sure it happens when the scope ends.
  • Loading branch information
MrStevns committed Nov 24, 2024
1 parent 3447d67 commit bb18358
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
6 changes: 6 additions & 0 deletions core_lib/src/graphics/vector/vectorimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
{
Expand Down
4 changes: 4 additions & 0 deletions core_lib/src/soundplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion core_lib/src/structure/filemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -957,7 964,6 @@ Status FileManager::rebuildMainXML(Object* object)
QTextStream fout(&file);
xmlDoc.save(fout, 2);
fout.flush();
file.close();

return Status::OK;
}
Expand Down
8 changes: 6 additions & 2 deletions core_lib/src/structure/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -665,14 667,16 @@ bool Object::importPalette(const QString& filePath)
{
return false;
}
ScopeGuard fileScope([&] {
file.close();
});

if (file.fileName().endsWith(".gpl", Qt::CaseInsensitive))
{
importPaletteGPL(file);
} else {
importPalettePencil(file);
}
file.close();
return true;
}

Expand Down

0 comments on commit bb18358

Please sign in to comment.