Skip to content

Commit

Permalink
Fix some memory leaks on packet sending.
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzhul committed Feb 21, 2015
1 parent 5a5854e commit 009149a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/client.cpp
Expand Up @@ -956,6 +956,7 @@ void Client::Send(NetworkPacket* pkt)
serverCommandFactoryTable[pkt->getCommand()].channel,
pkt,
serverCommandFactoryTable[pkt->getCommand()].reliable);
delete pkt;
}

void Client::interact(u8 action, const PointedThing& pointed)
Expand Down
1 change: 1 addition & 0 deletions src/network/connection.cpp
Expand Up @@ -1729,6 +1729,7 @@ void ConnectionSendThread::connect(Address address)
m_connection->SetPeerID(PEER_ID_INEXISTENT);
NetworkPacket* pkt = new NetworkPacket(0,0);
m_connection->Send(PEER_ID_SERVER, 0, pkt, true);
delete pkt;
}

void ConnectionSendThread::disconnect()
Expand Down
19 changes: 7 additions & 12 deletions src/server.cpp
Expand Up @@ -2079,24 +2079,19 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
float maxd = far_d_nodes*BS;
v3f p_f = intToFloat(p, BS);

NetworkPacket* pkt = new NetworkPacket(TOCLIENT_REMOVENODE, 2 + 2 + 2);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_REMOVENODE, 6);
*pkt << p;

std::list<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator
i = clients.begin();
i != clients.end(); ++i)
{
if(far_players)
{
i != clients.end(); ++i) {
if(far_players) {
// Get player
Player *player = m_env->getPlayer(*i);
if(player)
{
if(Player *player = m_env->getPlayer(*i)) {
// If player is far away, only set modified blocks not sent
v3f player_pos = player->getPosition();
if(player_pos.getDistanceFrom(p_f) > maxd)
{
if(player_pos.getDistanceFrom(p_f) > maxd) {
far_players->push_back(*i);
continue;
}
Expand All @@ -2118,7 +2113,7 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
v3f p_f = intToFloat(p, BS);

std::list<u16> clients = m_clients.getClientIDs();
for(std::list<u16>::iterator
for(std::list<u16>::iterator
i = clients.begin();
i != clients.end(); ++i)
{
Expand All @@ -2139,7 +2134,7 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id,
}
}

NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ADDNODE, 0);
NetworkPacket* pkt = new NetworkPacket(TOCLIENT_ADDNODE, 6 + 2 + 1 + 1 + 1);
m_clients.Lock();
RemoteClient* client = m_clients.lockedGetClientNoEx(*i);
if (client != 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/test.cpp
Expand Up @@ -2010,6 +2010,8 @@ struct TestConnection: public TestBase
<< std::endl;

UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);

delete pkt;
}

u16 peer_id_client = 2;
Expand Down Expand Up @@ -2074,6 +2076,8 @@ struct TestConnection: public TestBase

UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
UASSERT(peer_id == PEER_ID_SERVER);

delete pkt;
}

// Check peer handlers
Expand Down

0 comments on commit 009149a

Please sign in to comment.