2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

Fix memory leak

This commit is contained in:
Feng Chen 2021-10-25 18:55:20 +08:00
parent 3c8c17be4d
commit a8b0104923
4 changed files with 38 additions and 0 deletions

View file

@ -56,6 +56,7 @@ bool KHandleTable::Remove(Handle handle) {
} }
// Close the object. // Close the object.
kernel.UnregisterInUseObject(obj);
obj->Close(); obj->Close();
return true; return true;
} }

View file

@ -170,6 +170,17 @@ struct KernelCore::Impl {
// Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others // Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others
next_host_thread_id = Core::Hardware::NUM_CPU_CORES; next_host_thread_id = Core::Hardware::NUM_CPU_CORES;
// Close kernel objects that were not freed on shutdown
{
std::lock_guard lk(registered_in_use_objects_lock);
if (registered_in_use_objects.size()) {
for (auto thread : registered_in_use_objects) {
thread->Close();
}
registered_in_use_objects.clear();
}
}
// Track kernel objects that were not freed on shutdown // Track kernel objects that were not freed on shutdown
{ {
std::lock_guard lk(registered_objects_lock); std::lock_guard lk(registered_objects_lock);
@ -714,9 +725,11 @@ struct KernelCore::Impl {
std::unordered_set<KServerPort*> server_ports; std::unordered_set<KServerPort*> server_ports;
std::unordered_set<KServerSession*> server_sessions; std::unordered_set<KServerSession*> server_sessions;
std::unordered_set<KAutoObject*> registered_objects; std::unordered_set<KAutoObject*> registered_objects;
std::unordered_set<KAutoObject*> registered_in_use_objects;
std::mutex server_ports_lock; std::mutex server_ports_lock;
std::mutex server_sessions_lock; std::mutex server_sessions_lock;
std::mutex registered_objects_lock; std::mutex registered_objects_lock;
std::mutex registered_in_use_objects_lock;
std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor; std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor;
std::vector<Kernel::PhysicalCore> cores; std::vector<Kernel::PhysicalCore> cores;
@ -928,6 +941,16 @@ void KernelCore::UnregisterKernelObject(KAutoObject* object) {
impl->registered_objects.erase(object); impl->registered_objects.erase(object);
} }
void KernelCore::RegisterInUseObject(KAutoObject* object) {
std::lock_guard lk(impl->registered_in_use_objects_lock);
impl->registered_in_use_objects.insert(object);
}
void KernelCore::UnregisterInUseObject(KAutoObject* object) {
std::lock_guard lk(impl->registered_in_use_objects_lock);
impl->registered_in_use_objects.erase(object);
}
bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const { bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const {
return port != impl->named_ports.cend(); return port != impl->named_ports.cend();
} }

View file

@ -204,6 +204,14 @@ public:
/// destroyed during the current emulation session. /// destroyed during the current emulation session.
void UnregisterKernelObject(KAutoObject* object); void UnregisterKernelObject(KAutoObject* object);
/// Registers kernel objects with guest in use state, this is purely for close
/// after emulation has been shutdown.
void RegisterInUseObject(KAutoObject* object);
/// Unregisters a kernel object previously registered with RegisterInUseObject when it was
/// destroyed during the current emulation session.
void UnregisterInUseObject(KAutoObject* object);
/// Determines whether or not the given port is a valid named port. /// Determines whether or not the given port is a valid named port.
bool IsValidNamedPort(NamedPortTable::const_iterator port) const; bool IsValidNamedPort(NamedPortTable::const_iterator port) const;

View file

@ -427,11 +427,15 @@ static ResultCode WaitSynchronization(Core::System& system, s32* index, VAddr ha
R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(objs.data(), handles, R_UNLESS(handle_table.GetMultipleObjects<KSynchronizationObject>(objs.data(), handles,
num_handles), num_handles),
ResultInvalidHandle); ResultInvalidHandle);
for (const auto& obj : objs) {
kernel.RegisterInUseObject(obj);
}
} }
// Ensure handles are closed when we're done. // Ensure handles are closed when we're done.
SCOPE_EXIT({ SCOPE_EXIT({
for (u64 i = 0; i < num_handles; ++i) { for (u64 i = 0; i < num_handles; ++i) {
kernel.UnregisterInUseObject(objs[i]);
objs[i]->Close(); objs[i]->Close();
} }
}); });
@ -1544,6 +1548,7 @@ static ResultCode StartThread(Core::System& system, Handle thread_handle) {
// If we succeeded, persist a reference to the thread. // If we succeeded, persist a reference to the thread.
thread->Open(); thread->Open();
system.Kernel().RegisterInUseObject(thread.GetPointerUnsafe());
return ResultSuccess; return ResultSuccess;
} }
@ -1559,6 +1564,7 @@ static void ExitThread(Core::System& system) {
auto* const current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread(); auto* const current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread();
system.GlobalSchedulerContext().RemoveThread(current_thread); system.GlobalSchedulerContext().RemoveThread(current_thread);
current_thread->Exit(); current_thread->Exit();
system.Kernel().UnregisterInUseObject(current_thread);
} }
static void ExitThread32(Core::System& system) { static void ExitThread32(Core::System& system) {