Skip to content

Commit 43dce21

Browse files
committed
Replaced shared pointer with weak pointer to the socket reader to fix possible memory leak
1 parent b383f6a commit 43dce21

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/catapult/net/PacketReadersWriters.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace catapult { namespace net {
2828
ionet::Node Node;
2929
WeakSocketPointer pSocketWeak;
3030
std::shared_ptr<ionet::PacketIo> pBufferedIo;
31-
std::shared_ptr<ChainedSocketReader> pReader;
31+
std::weak_ptr<ChainedSocketReader> pReader;
3232
};
3333

3434
// expected sequences
@@ -38,6 +38,7 @@ namespace catapult { namespace net {
3838
class WriterContainer {
3939
private:
4040
using ReaderWriterContainer = std::unordered_map<Key, ReaderWriterState, utils::ArrayHasher<Key>>;
41+
using ChainedSocketReaderFactory = std::function<std::shared_ptr<ChainedSocketReader> (const SocketPointer&, const std::shared_ptr<ionet::PacketIo>&)>;
4142

4243
public:
4344
size_t size() const {
@@ -97,32 +98,42 @@ namespace catapult { namespace net {
9798
m_connectingNodeIdentityKeys.erase(node.identityKey());
9899
}
99100

100-
bool insert(const ReaderWriterState& state) {
101+
bool insert(net::ConnectionType connectionType, ionet::Node node, const SocketPointer& pSocket, const ChainedSocketReaderFactory& readerFactory) {
101102
std::unique_lock guard(m_mutex);
102103

103-
const auto& identityKey = state.Node.identityKey();
104-
switch (state.ConnectionType) {
104+
const auto& identityKey = node.identityKey();
105+
ReaderWriterState* pState = nullptr;
106+
switch (connectionType) {
105107
case ConnectionType::Connected: {
106108
if (!m_connectedNodeIdentityKeys.emplace(identityKey).second) {
107-
CATAPULT_LOG(debug) << "ignoring connection to already connected peer " << state.Node << " " << identityKey;
109+
CATAPULT_LOG(debug) << "ignoring connection to already connected peer " << node << " " << identityKey;
108110
return false;
109111
}
110112
m_connectingNodeIdentityKeys.erase(identityKey);
111-
m_connectedWriters[identityKey] = state;
113+
pState = &m_connectedWriters[identityKey];
112114
break;
113115
}
114116
case ConnectionType::Accepted: {
115117
if (!m_acceptedNodeIdentityKeys.emplace(identityKey).second) {
116-
CATAPULT_LOG(debug) << "not accepting already connected peer " << state.Node << " " << identityKey;
118+
CATAPULT_LOG(debug) << "not accepting already connected peer " << node << " " << identityKey;
117119
return false;
118120
}
119-
m_acceptedWriters[identityKey] = state;
121+
pState = &m_acceptedWriters[identityKey];
120122
break;
121123
}
122124
}
123125

124126
m_nodeIdentityKeys.emplace(identityKey);
125-
state.pReader->start();
127+
128+
pState->ConnectionType = connectionType;
129+
pState->Node = node;
130+
pState->pSocketWeak = pSocket;
131+
pState->pBufferedIo = pSocket->buffered();
132+
// the reader takes ownership of the socket
133+
auto pReader = readerFactory(pSocket, pState->pBufferedIo);
134+
pReader->start();
135+
pState->pReader = pReader;
136+
126137
return true;
127138
}
128139

@@ -320,15 +331,11 @@ namespace catapult { namespace net {
320331
}
321332

322333
bool addWriter(const ionet::Node& node, const ionet::SslPacketSocketInfo& socketInfo, ConnectionType connectionType) {
323-
ReaderWriterState state;
324-
state.ConnectionType = connectionType;
325-
state.Node = node;
326334
const auto& pSocket = socketInfo.socket();
327-
state.pSocketWeak = pSocket;
328-
state.pBufferedIo = pSocket->buffered();
329335
auto identity = ionet::ReaderIdentity{ node.identityKey(), socketInfo.host() };
330-
state.pReader = createReader(pSocket, state.pBufferedIo, identity, connectionType);
331-
return m_writers.insert(state);
336+
return m_writers.insert(connectionType, node, pSocket, [this, identity, connectionType](const auto& pSocket, const auto& pBufferedIo) {
337+
return this->createReader(pSocket, pBufferedIo, identity, connectionType);
338+
});
332339
}
333340

334341
void removeWriter(const WeakSocketPointer& pSocketWeak, const Key& identityKey, ConnectionType connectionType) {

0 commit comments

Comments
 (0)