Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix onNetworkMessage event memory leak #4887

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ void onInventoryUpdate(Player* player, Item* item, slots_t slot, bool equip)
scriptInterface.callVoidFunction(4);
}

void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage* msg)
void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage_ptr& msg)
{
// Player:onNetworkMessage(recvByte, msg)
if (playerHandlers.onNetworkMessage == -1) {
Expand All @@ -1403,7 +1403,7 @@ void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage* msg)

lua_pushnumber(L, recvByte);

tfs::lua::pushUserdata(L, msg);
tfs::lua::pushUserdata(L, msg.release());
tfs::lua::setMetatable(L, -1, "NetworkMessage");

scriptInterface.callVoidFunction(3);
Expand Down
4 changes: 2 additions & 2 deletions src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "const.h"
#include "creature.h"
#include "luascript.h"
#include "networkmessage.h"
nekiro marked this conversation as resolved.
Show resolved Hide resolved

class ItemType;
class NetworkMessage;
class Party;
class Spell;
class Tile;
Expand Down Expand Up @@ -83,7 +83,7 @@ void onLoseExperience(Player* player, uint64_t& exp);
void onGainSkillTries(Player* player, skills_t skill, uint64_t& tries);
void onWrapItem(Player* player, Item* item);
void onInventoryUpdate(Player* player, Item* item, slots_t slot, bool equip);
void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage* msg);
void onNetworkMessage(Player* player, uint8_t recvByte, NetworkMessage_ptr& msg);
bool onSpellCheck(Player* player, const Spell* spell);

} // namespace tfs::events::player
Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5548,7 +5548,7 @@ void Game::parsePlayerExtendedOpcode(uint32_t playerId, uint8_t opcode, const st
}
}

void Game::parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage* msg)
void Game::parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage_ptr msg)
maattch marked this conversation as resolved.
Show resolved Hide resolved
{
Player* player = getPlayerByID(playerId);
if (!player) {
Expand Down
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ class Game
void playerAcceptMarketOffer(uint32_t playerId, uint32_t timestamp, uint16_t counter, uint16_t amount);

void parsePlayerExtendedOpcode(uint32_t playerId, uint8_t opcode, const std::string& buffer);
void parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage* msg);
void parsePlayerNetworkMessage(uint32_t playerId, uint8_t recvByte, NetworkMessage_ptr msg);
MillhioreBT marked this conversation as resolved.
Show resolved Hide resolved

std::vector<Item*> getMarketItemList(uint16_t wareId, uint16_t sufficientCount, Player& player);

Expand Down
3 changes: 3 additions & 0 deletions src/networkmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
class Item;
struct Position;

class NetworkMessage;
using NetworkMessage_ptr = std::unique_ptr<NetworkMessage>;

class NetworkMessage
{
public:
Expand Down
4 changes: 3 additions & 1 deletion src/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,8 +793,10 @@ void ProtocolGame::parsePacket(NetworkMessage& msg)
// case 0xFE: break; // store window history 2

default:
// we cannot pass an unique_ptr as capture here because
// std::function requires the callable object to be *copyable*
g_dispatcher.addTask([=, playerID = player->getID(), msg = new NetworkMessage(msg)]() {
MillhioreBT marked this conversation as resolved.
Show resolved Hide resolved
g_game.parsePlayerNetworkMessage(playerID, recvbyte, msg);
g_game.parsePlayerNetworkMessage(playerID, recvbyte, NetworkMessage_ptr(msg));
MillhioreBT marked this conversation as resolved.
Show resolved Hide resolved
});
break;
}
Expand Down
Loading