diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2022-03-10 15:27:09 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2022-03-11 09:13:11 +0100 |
commit | 768f9422bc3b1349729b8a50feb8c0797003aee9 (patch) | |
tree | ccc2863deb916a604b39cf67f897b32edcdfc4da /editor | |
parent | 259114e9e0d5bb01d023ad978e06ed14ca785b1d (diff) |
Convert uses of `DirAccess *` to `DirAccessRef` to prevent memleaks
`DirAccess *` needs to be deleted manually, and this is often forgotten
especially when doing early returns with `ERR_FAIL_COND`.
`DirAccessRef` is deleted automatically when it goes out of scope.
Co-authored-by: bruvzg <7645683+bruvzg@users.noreply.github.com>
Diffstat (limited to 'editor')
-rw-r--r-- | editor/dependency_editor.cpp | 3 | ||||
-rw-r--r-- | editor/editor_asset_installer.cpp | 4 | ||||
-rw-r--r-- | editor/editor_file_system.cpp | 3 | ||||
-rw-r--r-- | editor/editor_fonts.cpp | 4 | ||||
-rw-r--r-- | editor/editor_settings.cpp | 9 | ||||
-rw-r--r-- | editor/export_template_manager.cpp | 3 | ||||
-rw-r--r-- | editor/filesystem_dock.cpp | 31 | ||||
-rw-r--r-- | editor/plugin_config_dialog.cpp | 2 | ||||
-rw-r--r-- | editor/project_manager.cpp | 32 | ||||
-rw-r--r-- | editor/script_create_dialog.cpp | 35 |
10 files changed, 42 insertions, 84 deletions
diff --git a/editor/dependency_editor.cpp b/editor/dependency_editor.cpp index 1802bdec93..95b3a02631 100644 --- a/editor/dependency_editor.cpp +++ b/editor/dependency_editor.cpp @@ -749,12 +749,11 @@ void OrphanResourcesDialog::_find_to_delete(TreeItem *p_item, List<String> &path } void OrphanResourcesDialog::_delete_confirm() { - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); for (const String &E : paths) { da->remove(E); EditorFileSystem::get_singleton()->update_file(E); } - memdelete(da); refresh(); } diff --git a/editor/editor_asset_installer.cpp b/editor/editor_asset_installer.cpp index 3835399c99..4a51f7c6c2 100644 --- a/editor/editor_asset_installer.cpp +++ b/editor/editor_asset_installer.cpp @@ -277,10 +277,8 @@ void EditorAssetInstaller::ok_pressed() { dirpath = dirpath.substr(0, dirpath.length() - 1); } - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); da->make_dir(dirpath); - memdelete(da); - } else { Vector<uint8_t> data; data.resize(info.uncompressed_size); diff --git a/editor/editor_file_system.cpp b/editor/editor_file_system.cpp index 0c46cebec0..45362896ca 100644 --- a/editor/editor_file_system.cpp +++ b/editor/editor_file_system.cpp @@ -1094,12 +1094,11 @@ void EditorFileSystem::_delete_internal_files(String p_file) { if (FileAccess::exists(p_file + ".import")) { List<String> paths; ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths); - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); for (const String &E : paths) { da->remove(E); } da->remove(p_file + ".import"); - memdelete(da); } } diff --git a/editor/editor_fonts.cpp b/editor/editor_fonts.cpp index d80ee1d569..853278c5fc 100644 --- a/editor/editor_fonts.cpp +++ b/editor/editor_fonts.cpp @@ -174,7 +174,7 @@ Ref<FontData> load_cached_internal_font(const uint8_t *p_data, size_t p_size, Te } void editor_register_fonts(Ref<Theme> p_theme) { - DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); /* Custom font */ @@ -236,8 +236,6 @@ void editor_register_fonts(Ref<Theme> p_theme) { EditorSettings::get_singleton()->set_manually("interface/editor/code_font", ""); } - memdelete(dir); - /* Noto Sans */ Ref<FontData> DefaultFont = load_cached_internal_font(_font_NotoSans_Regular, _font_NotoSans_Regular_size, font_hinting, font_antialiased, true, font_subpixel_positioning); diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 45685b8fb2..179a88dc60 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -1222,7 +1222,7 @@ bool EditorSettings::is_dark_theme() { void EditorSettings::list_text_editor_themes() { String themes = "Default,Godot 2,Custom"; - DirAccess *d = DirAccess::open(get_text_editor_themes_dir()); + DirAccessRef d = DirAccess::open(get_text_editor_themes_dir()); if (d) { List<String> custom_themes; d->list_dir_begin(); @@ -1234,7 +1234,6 @@ void EditorSettings::list_text_editor_themes() { file = d->get_next(); } d->list_dir_end(); - memdelete(d); custom_themes.sort(); for (const String &E : custom_themes) { @@ -1289,10 +1288,9 @@ bool EditorSettings::import_text_editor_theme(String p_file) { return false; } - DirAccess *d = DirAccess::open(get_text_editor_themes_dir()); + DirAccessRef d = DirAccess::open(get_text_editor_themes_dir()); if (d) { d->copy(p_file, get_text_editor_themes_dir().plus_file(p_file.get_file())); - memdelete(d); return true; } } @@ -1342,7 +1340,7 @@ Vector<String> EditorSettings::get_script_templates(const String &p_extension, c if (!p_custom_path.is_empty()) { template_dir = p_custom_path; } - DirAccess *d = DirAccess::open(template_dir); + DirAccessRef d = DirAccess::open(template_dir); if (d) { d->list_dir_begin(); String file = d->get_next(); @@ -1353,7 +1351,6 @@ Vector<String> EditorSettings::get_script_templates(const String &p_extension, c file = d->get_next(); } d->list_dir_end(); - memdelete(d); } return templates; } diff --git a/editor/export_template_manager.cpp b/editor/export_template_manager.cpp index 0a8d35aff1..df3e73267e 100644 --- a/editor/export_template_manager.cpp +++ b/editor/export_template_manager.cpp @@ -44,7 +44,7 @@ void ExportTemplateManager::_update_template_status() { // Fetch installed templates from the file system. - DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); const String &templates_dir = EditorSettings::get_singleton()->get_templates_dir(); Error err = da->change_dir(templates_dir); @@ -62,7 +62,6 @@ void ExportTemplateManager::_update_template_status() { } } da->list_dir_end(); - memdelete(da); // Update the state of the current version. String current_version = VERSION_FULL_CONFIG; diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index e8a2a46dd2..58074207ff 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -525,16 +525,14 @@ void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_fa if (target_path.ends_with("/")) { target_path = target_path.substr(0, target_path.length() - 1); } - DirAccess *dirAccess = DirAccess::create(DirAccess::ACCESS_RESOURCES); - if (dirAccess->file_exists(p_path)) { + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + if (da->file_exists(p_path)) { path = target_path; - } else if (dirAccess->dir_exists(p_path)) { + } else if (da->dir_exists(p_path)) { path = target_path + "/"; } else { - memdelete(dirAccess); ERR_FAIL_MSG(vformat("Cannot navigate to '%s' as it has not been found in the file system!", p_path)); } - memdelete(dirAccess); } _set_current_path_text(path); @@ -1171,7 +1169,7 @@ void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_ _get_all_items_in_dir(EditorFileSystem::get_singleton()->get_filesystem_path(old_path), file_changed_paths, folder_changed_paths); } - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); print_verbose("Moving " + old_path + " -> " + new_path); Error err = da->rename(old_path, new_path); if (err == OK) { @@ -1211,7 +1209,6 @@ void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_ } else { EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + "\n"); } - memdelete(da); } void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const String &p_new_path) const { @@ -1230,7 +1227,7 @@ void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const Strin return; } - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); print_verbose("Duplicating " + old_path + " -> " + new_path); Error err = p_item.is_file ? da->copy(old_path, new_path) : da->copy_dir(old_path, new_path); if (err == OK) { @@ -1268,7 +1265,6 @@ void FileSystemDock::_try_duplicate_item(const FileOrFolder &p_item, const Strin } else { EditorNode::get_singleton()->add_io_error(TTR("Error duplicating:") + "\n" + old_path + "\n"); } - memdelete(da); } void FileSystemDock::_update_resource_paths_after_move(const Map<String, String> &p_renames) const { @@ -1418,12 +1414,11 @@ void FileSystemDock::_make_dir_confirm() { directory = directory.get_base_dir(); } print_verbose("Making folder " + dir_name + " in " + directory); - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); Error err = da->change_dir(directory); if (err == OK) { err = da->make_dir(dir_name); } - memdelete(da); if (err == OK) { print_verbose("FileSystem: calling rescan."); @@ -1464,13 +1459,11 @@ void FileSystemDock::_make_scene_confirm() { scene_name = directory.plus_file(scene_name); - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); if (da->file_exists(scene_name)) { EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists.")); - memdelete(da); return; } - memdelete(da); int idx = EditorNode::get_singleton()->new_scene(); EditorNode::get_singleton()->get_editor_data().set_scene_path(idx, scene_name); @@ -1533,7 +1526,7 @@ void FileSystemDock::_rename_operation_confirm() { } // Present a more user friendly warning for name conflict. - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); #if defined(WINDOWS_ENABLED) || defined(UWP_ENABLED) // Workaround case insensitivity on Windows. if ((da->file_exists(new_path) || da->dir_exists(new_path)) && new_path.to_lower() != old_path.to_lower()) { @@ -1541,10 +1534,8 @@ void FileSystemDock::_rename_operation_confirm() { if (da->file_exists(new_path) || da->dir_exists(new_path)) { #endif EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists.")); - memdelete(da); return; } - memdelete(da); Map<String, String> file_renames; Map<String, String> folder_renames; @@ -1588,13 +1579,11 @@ void FileSystemDock::_duplicate_operation_confirm() { String new_path = base_dir.plus_file(new_name); // Present a more user friendly warning for name conflict - DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); if (da->file_exists(new_path) || da->dir_exists(new_path)) { EditorNode::get_singleton()->show_warning(TTR("A file or folder with this name already exists.")); - memdelete(da); return; } - memdelete(da); _try_duplicate_item(to_duplicate, new_path); @@ -2818,7 +2807,7 @@ void FileSystemDock::_get_imported_files(const String &p_path, Vector<String> &f return; } - DirAccess *da = DirAccess::open(p_path); + DirAccessRef da = DirAccess::open(p_path); da->list_dir_begin(); String n = da->get_next(); while (!n.is_empty()) { diff --git a/editor/plugin_config_dialog.cpp b/editor/plugin_config_dialog.cpp index d5e7c312d9..02cc95e14a 100644 --- a/editor/plugin_config_dialog.cpp +++ b/editor/plugin_config_dialog.cpp @@ -50,7 +50,7 @@ void PluginConfigDialog::_on_confirmed() { String path = "res://addons/" + subfolder_edit->get_text(); if (!_edit_mode) { - DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef d = DirAccess::create(DirAccess::ACCESS_RESOURCES); if (!d || d->make_dir_recursive(path) != OK) { return; } diff --git a/editor/project_manager.cpp b/editor/project_manager.cpp index 78f6fe58d0..35902fd354 100644 --- a/editor/project_manager.cpp +++ b/editor/project_manager.cpp @@ -147,7 +147,7 @@ private: } String _test_path() { - DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); String valid_path, valid_install_path; if (d->change_dir(project_path->get_text()) == OK) { valid_path = project_path->get_text(); @@ -165,7 +165,6 @@ private: if (valid_path.is_empty()) { set_message(TTR("The path specified doesn't exist."), MESSAGE_ERROR); - memdelete(d); get_ok_button()->set_disabled(true); return ""; } @@ -179,7 +178,6 @@ private: if (valid_install_path.is_empty()) { set_message(TTR("The path specified doesn't exist."), MESSAGE_ERROR, INSTALL_PATH); - memdelete(d); get_ok_button()->set_disabled(true); return ""; } @@ -194,7 +192,6 @@ private: unzFile pkg = unzOpen2(valid_path.utf8().get_data(), &io); if (!pkg) { set_message(TTR("Error opening package file (it's not in ZIP format)."), MESSAGE_ERROR); - memdelete(d); get_ok_button()->set_disabled(true); unzClose(pkg); return ""; @@ -215,7 +212,6 @@ private: if (ret == UNZ_END_OF_LIST_OF_FILE) { set_message(TTR("Invalid \".zip\" project file; it doesn't contain a \"project.godot\" file."), MESSAGE_ERROR); - memdelete(d); get_ok_button()->set_disabled(true); unzClose(pkg); return ""; @@ -242,14 +238,12 @@ private: if (!is_folder_empty) { set_message(TTR("Please choose an empty folder."), MESSAGE_WARNING, INSTALL_PATH); - memdelete(d); get_ok_button()->set_disabled(true); return ""; } } else { set_message(TTR("Please choose a \"project.godot\" or \".zip\" file."), MESSAGE_ERROR); - memdelete(d); install_path_container->hide(); get_ok_button()->set_disabled(true); return ""; @@ -257,7 +251,6 @@ private: } else if (valid_path.ends_with("zip")) { set_message(TTR("This directory already contains a Godot project."), MESSAGE_ERROR, INSTALL_PATH); - memdelete(d); get_ok_button()->set_disabled(true); return ""; } @@ -282,7 +275,6 @@ private: if (!is_folder_empty) { set_message(TTR("The selected path is not empty. Choosing an empty folder is highly recommended."), MESSAGE_WARNING); - memdelete(d); get_ok_button()->set_disabled(false); return valid_path; } @@ -290,7 +282,6 @@ private: set_message(""); set_message("", MESSAGE_SUCCESS, INSTALL_PATH); - memdelete(d); get_ok_button()->set_disabled(false); return valid_path; } @@ -389,7 +380,7 @@ private: return; } - DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); if (d->change_dir(project_path->get_text()) == OK) { if (!d->dir_exists(project_name_no_edges)) { if (d->make_dir(project_name_no_edges) == OK) { @@ -408,8 +399,6 @@ private: dialog_error->popup_centered(); } } - - memdelete(d); } void _text_changed(const String &p_text) { @@ -551,14 +540,11 @@ private: if (path.is_empty() || path == zip_root || !zip_root.is_subsequence_of(path)) { // } else if (path.ends_with("/")) { // a dir - path = path.substr(0, path.length() - 1); String rel_path = path.substr(zip_root.length()); - DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); da->make_dir(dir.plus_file(rel_path)); - memdelete(da); - } else { Vector<uint8_t> data; data.resize(info.uncompressed_size); @@ -620,9 +606,8 @@ private: void _remove_created_folder() { if (!created_folder_path.is_empty()) { - DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); d->remove(created_folder_path); - memdelete(d); create_dir->set_disabled(false); created_folder_path = ""; @@ -725,10 +710,9 @@ public: project_path->set_text(fav_dir); fdialog->set_current_dir(fav_dir); } else { - DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); project_path->set_text(d->get_current_dir()); fdialog->set_current_dir(d->get_current_dir()); - memdelete(d); } String proj = TTR("New Game Project"); project_name->set_text(proj); @@ -2411,12 +2395,11 @@ void ProjectManager::_files_dropped(PackedStringArray p_files, int p_screen) { return; } Set<String> folders_set; - DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); for (int i = 0; i < p_files.size(); i++) { String file = p_files[i]; folders_set.insert(da->dir_exists(file) ? file : file.get_base_dir()); } - memdelete(da); if (folders_set.size() > 0) { PackedStringArray folders; for (Set<String>::Element *E = folders_set.front(); E; E = E->next()) { @@ -2425,7 +2408,7 @@ void ProjectManager::_files_dropped(PackedStringArray p_files, int p_screen) { bool confirm = true; if (folders.size() == 1) { - DirAccess *dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); if (dir->change_dir(folders[0]) == OK) { dir->list_dir_begin(); String file = dir->get_next(); @@ -2437,7 +2420,6 @@ void ProjectManager::_files_dropped(PackedStringArray p_files, int p_screen) { } dir->list_dir_end(); } - memdelete(dir); } if (confirm) { multi_scan_ask->get_ok_button()->disconnect("pressed", callable_mp(this, &ProjectManager::_scan_multiple_folders)); diff --git a/editor/script_create_dialog.cpp b/editor/script_create_dialog.cpp index bf43e11cdb..055b30dae2 100644 --- a/editor/script_create_dialog.cpp +++ b/editor/script_create_dialog.cpp @@ -247,23 +247,22 @@ String ScriptCreateDialog::_validate_path(const String &p_path, bool p_file_must return TTR("Path is not local."); } - DirAccess *d = DirAccess::create(DirAccess::ACCESS_RESOURCES); - if (d->change_dir(p.get_base_dir()) != OK) { - memdelete(d); - return TTR("Base path is invalid."); + { + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + if (da->change_dir(p.get_base_dir()) != OK) { + return TTR("Base path is invalid."); + } } - memdelete(d); - // Check if file exists. - DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES); - if (f->dir_exists(p)) { - memdelete(f); - return TTR("A directory with the same name exists."); - } else if (p_file_must_exist && !f->file_exists(p)) { - memdelete(f); - return TTR("File does not exist."); + { + // Check if file exists. + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); + if (da->dir_exists(p)) { + return TTR("A directory with the same name exists."); + } else if (p_file_must_exist && !da->file_exists(p)) { + return TTR("File does not exist."); + } } - memdelete(f); // Check file extension. String extension = p.get_extension(); @@ -556,13 +555,12 @@ void ScriptCreateDialog::_path_changed(const String &p_path) { } // Check if file exists. - DirAccess *f = DirAccess::create(DirAccess::ACCESS_RESOURCES); + DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES); String p = ProjectSettings::get_singleton()->localize_path(p_path.strip_edges()); - if (f->file_exists(p)) { + if (da->file_exists(p)) { is_new_script_created = false; _msg_path_valid(true, TTR("File exists, it will be reused.")); } - memdelete(f); is_path_valid = true; _update_dialog(); @@ -838,7 +836,7 @@ Vector<ScriptLanguage::ScriptTemplate> ScriptCreateDialog::_get_user_templates(c String dir_path = p_dir.plus_file(p_object); - DirAccess *d = DirAccess::open(dir_path); + DirAccessRef d = DirAccess::open(dir_path); if (d) { d->list_dir_begin(); String file = d->get_next(); @@ -849,7 +847,6 @@ Vector<ScriptLanguage::ScriptTemplate> ScriptCreateDialog::_get_user_templates(c file = d->get_next(); } d->list_dir_end(); - memdelete(d); } return user_templates; } |