Prevent memory leaks when clearing AdhocMatching events.

This commit is contained in:
ANR2ME 2020-10-23 00:45:08 +07:00
parent bff18c07d0
commit 22419bfc6c

View File

@ -848,6 +848,19 @@ int WaitBlockingAdhocSocket(u64 threadSocketId, int type, int pspSocketId, void*
return ERROR_NET_ADHOC_TIMEOUT; return ERROR_NET_ADHOC_TIMEOUT;
} }
// Using matchingId = -1 to delete all matching events
void deleteMatchingEvents(const int matchingId = -1) {
for (auto it = matchingEvents.begin(); it != matchingEvents.end(); ) {
if (matchingId < 0 || it->data[0] == matchingId) {
if (Memory::IsValidAddress(it->data[2]))
userMemory.Free(it->data[2]);
it = matchingEvents.erase(it);
}
else
++it;
}
}
void netAdhocValidateLoopMemory() { void netAdhocValidateLoopMemory() {
// Allocate Memory if it wasn't valid/allocated after loaded from old SaveState // Allocate Memory if it wasn't valid/allocated after loaded from old SaveState
if (!dummyThreadHackAddr || (dummyThreadHackAddr && strcmp("dummythreadhack", kernelMemory.GetBlockTag(dummyThreadHackAddr)) != 0)) { if (!dummyThreadHackAddr || (dummyThreadHackAddr && strcmp("dummythreadhack", kernelMemory.GetBlockTag(dummyThreadHackAddr)) != 0)) {
@ -950,11 +963,11 @@ void __NetAdhocDoState(PointerWrap &p) {
if (p.mode == p.MODE_READ) { if (p.mode == p.MODE_READ) {
// Discard leftover events // Discard leftover events
adhocctlEvents.clear(); adhocctlEvents.clear();
matchingEvents.clear();
adhocctlRequests.clear(); adhocctlRequests.clear();
adhocSocketRequests.clear(); adhocSocketRequests.clear();
sendTargetPeers.clear(); sendTargetPeers.clear();
deleteAllAdhocSockets(); deleteAllAdhocSockets();
deleteMatchingEvents();
// Let's not change "Inited" value when Loading SaveState to prevent memory & port leaks // Let's not change "Inited" value when Loading SaveState to prevent memory & port leaks
netAdhocMatchingInited = cur_netAdhocMatchingInited; netAdhocMatchingInited = cur_netAdhocMatchingInited;
@ -4194,12 +4207,7 @@ int NetAdhocMatching_Delete(int matchingId) {
item = NULL; item = NULL;
// Making sure there are no leftover matching events from this session which could cause a crash on the next session // Making sure there are no leftover matching events from this session which could cause a crash on the next session
for (auto it = matchingEvents.begin(); it != matchingEvents.end(); ) { deleteMatchingEvents(matchingId);
if (it->data[0] == matchingId)
it = matchingEvents.erase(it);
else
++it;
}
// Stop Search // Stop Search
break; break;
@ -4238,7 +4246,7 @@ int sceNetAdhocMatchingInit(u32 memsize) {
fakePoolSize = memsize; fakePoolSize = memsize;
// Initialize Library // Initialize Library
matchingEvents.clear(); deleteMatchingEvents();
netAdhocMatchingInited = true; netAdhocMatchingInited = true;
// Return Success // Return Success