From 1e49d8f7abe32963710d016d589a5d3608f9f754 Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 27 Jul 2017 14:32:42 -0500 Subject: [PATCH] Fixed small memory leak --- conn/session.c | 2 ++ include/libp2p/net/stream.h | 2 ++ net/multistream.c | 10 +++++++ peer/peer.c | 56 ++++++++++++++++++++----------------- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/conn/session.c b/conn/session.c index 5d312c0..a50cc01 100644 --- a/conn/session.c +++ b/conn/session.c @@ -42,6 +42,8 @@ struct SessionContext* libp2p_session_context_new() { int libp2p_session_context_free(struct SessionContext* context) { if (context != NULL) { + if (context->default_stream != NULL) + context->default_stream->close(context); //TODO: Be more intelligent free(context); } diff --git a/include/libp2p/net/stream.h b/include/libp2p/net/stream.h index b04cb8d..93c92a9 100644 --- a/include/libp2p/net/stream.h +++ b/include/libp2p/net/stream.h @@ -31,6 +31,8 @@ struct Stream { /** * Closes a stream + * + * NOTE: This is also responsible for deallocating the Stream struct * @param stream the stream context * @returns true(1) on success, otherwise false(0) */ diff --git a/net/multistream.c b/net/multistream.c index 5ce5a1e..9f860b3 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -20,11 +20,21 @@ int multistream_default_timeout = 5; * An implementation of the libp2p multistream */ +/*** + * Close the Multistream interface + * NOTE: This also closes the socket + * @param stream_context a SessionContext + * @returns true(1) + */ int libp2p_net_multistream_close(void* stream_context) { struct SessionContext* secure_context = (struct SessionContext*)stream_context; struct Stream* stream = secure_context->insecure_stream; int socket_descriptor = *((int*)stream->socket_descriptor); close(socket_descriptor); + free(stream->socket_descriptor); + if (stream->address != NULL) + multiaddress_free(stream->address); + free(stream); return 1; } diff --git a/peer/peer.c b/peer/peer.c index 89de6fc..4eaf333 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -43,6 +43,36 @@ struct Libp2pPeer* libp2p_peer_new_from_multiaddress(const struct MultiAddress* return out; } +/*** + * Free resources of a Libp2pPeer + * @param in the struct to free + */ +void libp2p_peer_free(struct Libp2pPeer* in) { + if (in != NULL) { + if (in->addr_head != NULL && in->addr_head->item != NULL) { + libp2p_logger_debug("peer", "Freeing peer %s\n", ((struct MultiAddress*)in->addr_head->item)->string); + } else { + libp2p_logger_debug("peer", "Freeing peer with no multiaddress.\n"); + } + if (in->id != NULL) + free(in->id); + if (in->sessionContext != NULL) { + libp2p_session_context_free(in->sessionContext); + //libp2p_net_multistream_stream_free(in->connection); + in->sessionContext = NULL; + } + // free the memory in the linked list + struct Libp2pLinkedList* current = in->addr_head; + while (current != NULL) { + struct Libp2pLinkedList* temp = current->next; + multiaddress_free((struct MultiAddress*)current->item); + free(current); + current = temp; + } + free(in); + } +} + /** * Attempt to connect to the peer, setting connection_type correctly * NOTE: If successful, this will set peer->connection to the stream @@ -105,32 +135,6 @@ struct Libp2pPeer* libp2p_peer_new_from_data(const char* id, size_t id_size, con } */ -void libp2p_peer_free(struct Libp2pPeer* in) { - if (in != NULL) { - if (in->addr_head != NULL && in->addr_head->item != NULL) { - libp2p_logger_debug("peer", "Freeing peer %s\n", ((struct MultiAddress*)in->addr_head->item)->string); - } else { - libp2p_logger_debug("peer", "Freeing peer with no multiaddress.\n"); - } - if (in->id != NULL) - free(in->id); - if (in->sessionContext != NULL) { - libp2p_session_context_free(in->sessionContext); - //libp2p_net_multistream_stream_free(in->connection); - in->sessionContext = NULL; - } - // free the memory in the linked list - struct Libp2pLinkedList* current = in->addr_head; - while (current != NULL) { - struct Libp2pLinkedList* temp = current->next; - multiaddress_free((struct MultiAddress*)current->item); - free(current); - current = temp; - } - free(in); - } -} - /** * Make a copy of a peer *