[Kernel] (Partially) fix module refcounting

.xex module handles were retained twice in several places,
possibly causing them to leak.
More placed may have to be fixed too.
This commit is contained in:
Silent 2019-09-02 21:44:43 +02:00 committed by Rick Gibbed
parent f278e30d21
commit 9d48e904da
2 changed files with 7 additions and 7 deletions

View File

@ -367,8 +367,7 @@ object_ref<UserModule> KernelState::LoadUserModule(const char* raw_name,
// See if we've already loaded it // See if we've already loaded it
for (auto& existing_module : user_modules_) { for (auto& existing_module : user_modules_) {
if (existing_module->path() == path) { if (existing_module->path() == path) {
existing_module->Retain(); return existing_module;
return retain_object(existing_module.get());
} }
} }
@ -378,14 +377,13 @@ object_ref<UserModule> KernelState::LoadUserModule(const char* raw_name,
module = object_ref<UserModule>(new UserModule(this)); module = object_ref<UserModule>(new UserModule(this));
X_STATUS status = module->LoadFromFile(path); X_STATUS status = module->LoadFromFile(path);
if (XFAILED(status)) { if (XFAILED(status)) {
object_table()->RemoveHandle(module->handle()); object_table()->ReleaseHandle(module->handle());
return nullptr; return nullptr;
} }
global_lock.lock(); global_lock.lock();
// Retain when putting into the listing. // Putting into the listing automatically retains.
module->Retain();
user_modules_.push_back(module); user_modules_.push_back(module);
} }

View File

@ -96,8 +96,10 @@ dword_result_t XexLoadImage(lpstring_t module_name, dword_t module_flags,
// Not found; attempt to load as a user module. // Not found; attempt to load as a user module.
auto user_module = kernel_state()->LoadUserModule(module_name); auto user_module = kernel_state()->LoadUserModule(module_name);
if (user_module) { if (user_module) {
user_module->Retain(); // Give up object ownership, this reference will be released by the last
hmodule = user_module->hmodule_ptr(); // XexUnloadImage call
auto user_module_raw = user_module.release();
hmodule = user_module_raw->hmodule_ptr();
result = X_STATUS_SUCCESS; result = X_STATUS_SUCCESS;
} }
} }