Skip to content

Commit

Permalink
Use RAII for GFile
Browse files Browse the repository at this point in the history
  • Loading branch information
jhilmer committed Apr 7, 2023
1 parent 1205956 commit 645e548
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/core/control/xojfile/LoadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,9 @@ void LoadHandler::parseAudio() {
const char* filename = LoadHandlerHelper::getAttrib("fn", false, this);

GFileIOStream* fileStream = nullptr;
GFile* tmpFile = g_file_new_tmp("xournal_audio_XXXXXX.tmp", &fileStream, nullptr);
if (!tmpFile) {
xoj::util::GObjectSPtr<GFile> tmpFile(g_file_new_tmp("xournal_audio_XXXXXX.tmp", &fileStream, nullptr),
xoj::util::adopt);
if (!tmpFile.get()) {
g_warning("Unable to create temporary file for audio attachment.");
return;
}
Expand Down Expand Up @@ -840,7 +841,6 @@ void LoadHandler::parseAudio() {
while (readBytes < length) {
zip_int64_t read = zip_fread(attachmentFile, data, 1024);
if (read == -1) {
g_object_unref(tmpFile);
g_free(data);
zip_fclose(attachmentFile);
error("%s", FC(_F("Could not open attachment: {1}. Error message: Could not read file") % filename));
Expand All @@ -850,7 +850,6 @@ void LoadHandler::parseAudio() {
gboolean writeSuccessful =
g_output_stream_write_all(outputStream, data, static_cast<gsize>(read), nullptr, nullptr, nullptr);
if (!writeSuccessful) {
g_object_unref(tmpFile);
g_free(data);
zip_fclose(attachmentFile);
error("%s", FC(_F("Could not open attachment: {1}. Error message: Could not write file") % filename));
Expand All @@ -862,7 +861,7 @@ void LoadHandler::parseAudio() {
g_free(data);
zip_fclose(attachmentFile);

g_hash_table_insert(this->audioFiles, g_strdup(filename), g_file_get_path(tmpFile));
g_hash_table_insert(this->audioFiles, g_strdup(filename), g_file_get_path(tmpFile.get()));
}

void LoadHandler::parserStartElement(GMarkupParseContext* context, const gchar* elementName,
Expand Down

0 comments on commit 645e548

Please sign in to comment.