Fix memory leak when move to trash fails on Linux

(cherry picked from commit 8be49838b3)
This commit is contained in:
Haoyu Qiu 2022-01-25 18:38:13 +08:00 committed by Rémi Verschelde
parent 941879f601
commit f8afd16c89
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -3887,7 +3887,7 @@ Error OS_X11::move_to_trash(const String &p_path) {
// Create needed directories for decided trash can location. // Create needed directories for decided trash can location.
{ {
DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); DirAccessRef dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = dir_access->make_dir_recursive(trash_path); Error err = dir_access->make_dir_recursive(trash_path);
// Issue an error if trash can is not created proprely. // Issue an error if trash can is not created proprely.
@ -3896,7 +3896,6 @@ Error OS_X11::move_to_trash(const String &p_path) {
ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/files"); ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/files");
err = dir_access->make_dir_recursive(trash_path + "/info"); err = dir_access->make_dir_recursive(trash_path + "/info");
ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/info"); ERR_FAIL_COND_V_MSG(err != OK, err, "Could not create the trash path \"" + trash_path + "\"/info");
memdelete(dir_access);
} }
// The trash can is successfully created, now we check that we don't exceed our file name length limit. // The trash can is successfully created, now we check that we don't exceed our file name length limit.
@ -3936,16 +3935,15 @@ Error OS_X11::move_to_trash(const String &p_path) {
String trash_info = "[Trash Info]\nPath=" + p_path.http_escape() + "\nDeletionDate=" + timestamp + "\n"; String trash_info = "[Trash Info]\nPath=" + p_path.http_escape() + "\nDeletionDate=" + timestamp + "\n";
{ {
Error err; Error err;
FileAccess *file = FileAccess::open(trash_path + "/info/" + file_name + ".trashinfo", FileAccess::WRITE, &err); FileAccessRef file = FileAccess::open(trash_path + "/info/" + file_name + ".trashinfo", FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err != OK, err, "Can't create trashinfo file:" + trash_path + "/info/" + file_name + ".trashinfo"); ERR_FAIL_COND_V_MSG(err != OK, err, "Can't create trashinfo file:" + trash_path + "/info/" + file_name + ".trashinfo");
file->store_string(trash_info); file->store_string(trash_info);
file->close(); file->close();
// Rename our resource before moving it to the trash can. // Rename our resource before moving it to the trash can.
DirAccess *dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); DirAccessRef dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
err = dir_access->rename(p_path, p_path.get_base_dir() + "/" + file_name); err = dir_access->rename(p_path, p_path.get_base_dir() + "/" + file_name);
ERR_FAIL_COND_V_MSG(err != OK, err, "Can't rename file \"" + p_path + "\""); ERR_FAIL_COND_V_MSG(err != OK, err, "Can't rename file \"" + p_path + "\"");
memdelete(dir_access);
} }
// Move the given resource to the trash can. // Move the given resource to the trash can.