Skip to content

Commit e97b58c

Browse files
committed
Fixed private instance memory leaks.
1 parent 55be58c commit e97b58c

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/ChunkManager.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,19 @@ void ChunkManager::destroyInstance(uint64_t instanceID) {
236236
destroyChunk(coords);
237237
}
238238
}
239+
240+
void ChunkManager::destroyInstanceIfEmpty(uint64_t instanceID) {
241+
if (PLAYERID(instanceID) == 0)
242+
return; // don't clean up overworld/IZ chunks
243+
244+
std::vector<std::tuple<int, int, uint64_t>> sourceChunkCoords = getChunksInMap(instanceID);
245+
246+
for (std::tuple<int, int, uint64_t>& coords : sourceChunkCoords) {
247+
Chunk* chunk = chunks[coords];
248+
249+
if (chunk->players.size() > 0)
250+
return; // there are still players inside
251+
}
252+
253+
destroyInstance(instanceID);
254+
}

src/ChunkManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ namespace ChunkManager {
4040

4141
void createInstance(uint64_t);
4242
void destroyInstance(uint64_t);
43+
void destroyInstanceIfEmpty(uint64_t);
4344
}

src/NPCManager.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -628,16 +628,7 @@ void NPCManager::handleWarp(CNSocket* sock, int32_t warpId) {
628628
}
629629

630630
// post-warp: check if the source instance has no more players in it and delete it if so
631-
if (PLAYERID(fromInstance) == 0)
632-
return; // don't clean up overworld/IZ chunks
633-
634-
std::vector<std::tuple<int, int, uint64_t>> sourceChunkCoords = ChunkManager::getChunksInMap(fromInstance);
635-
for (std::tuple<int, int, uint64_t>& coords : sourceChunkCoords) {
636-
Chunk* chunk = ChunkManager::chunks[coords];
637-
if (chunk->players.size() > 0)
638-
return; // there are still players inside
639-
}
640-
ChunkManager::destroyInstance(fromInstance);
631+
ChunkManager::destroyInstanceIfEmpty(fromInstance);
641632
}
642633

643634
/*

src/PlayerManager.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,14 @@ void PlayerManager::addPlayer(CNSocket* key, Player plr) {
6464

6565
void PlayerManager::removePlayer(CNSocket* key) {
6666
PlayerView& view = players[key];
67+
uint64_t fromInstance = view.plr->instanceID;
6768

6869
//MissionManager::failInstancedMissions(key); moved to enter
6970
GroupManager::groupKickPlayer(view.plr);
7071

7172
// save player to DB
7273
Database::updatePlayer(view.plr);
7374

74-
INITSTRUCT(sP_FE2CL_PC_EXIT, exitPacket);
75-
exitPacket.iID = players[key].plr->iID;
76-
7775
// remove players from all chunks
7876
removePlayerFromChunks(view.currentChunks, key);
7977

@@ -87,6 +85,9 @@ void PlayerManager::removePlayer(CNSocket* key) {
8785
delete view.plr;
8886
players.erase(key);
8987

88+
// if the player was in a lair, clean it up
89+
ChunkManager::destroyInstanceIfEmpty(fromInstance);
90+
9091
std::cout << players.size() << " players" << std::endl;
9192
}
9293

@@ -230,6 +231,9 @@ void PlayerManager::updatePlayerChunk(CNSocket* sock, int X, int Y, uint64_t ins
230231
void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z, uint64_t I) {
231232
PlayerView& plrv = PlayerManager::players[sock];
232233
Player* plr = plrv.plr;
234+
235+
uint64_t fromInstance = plr->instanceID;
236+
233237
plr->instanceID = I;
234238
if (I != INSTANCE_OVERWORLD) {
235239
INITSTRUCT(sP_FE2CL_INSTANCE_MAP_INFO, pkt);
@@ -250,7 +254,8 @@ void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z, uint64_t I
250254
plrv.currentChunks.clear();
251255
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_WARP_USE_NPC_SUCC, sizeof(sP_FE2CL_REP_PC_WARP_USE_NPC_SUCC));
252256
}
253-
257+
258+
ChunkManager::destroyInstanceIfEmpty(fromInstance);
254259
}
255260

256261
void PlayerManager::sendPlayerTo(CNSocket* sock, int X, int Y, int Z) {

0 commit comments

Comments
 (0)