forked from Oskar1121/Ravendawn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQ4.cpp
36 lines (31 loc) · 1023 Bytes
/
Q4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Q4 - Assume all method calls work fine. Fix the memory leak issue in below method
void Game::addItemToPlayer(const std::string& recipient, uint16_t itemId)
{
Player* player = g_game.getPlayerByName(recipient);
if (!player) {
player = new Player(nullptr);
if (!player) {
// Failed to allocate memory
return;
}
if (!IOLoginData::loadPlayerByName(player, recipient)) {
// We did not load the player properly, we need to release the memory
delete player;
return;
}
}
Item* item = Item::CreateItem(itemId);
if (!item) {
if (player->isOffline()) {
// Release the memory
delete player;
}
return;
}
g_game.internalAddItem(player->getInbox(), item, INDEX_WHEREEVER, FLAG_NOLIMIT);
if (player->isOffline()) {
IOLoginData::savePlayer(player);
// Release the memory
delete player;
}
}