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>
This commit is contained in:
Rémi Verschelde 2022-03-10 15:27:09 +01:00
parent 259114e9e0
commit 768f9422bc
28 changed files with 154 additions and 259 deletions

View file

@ -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;
}