Fixed small memory leak
This commit is contained in:
parent
1971e60438
commit
1e49d8f7ab
4 changed files with 44 additions and 26 deletions
|
@ -42,6 +42,8 @@ struct SessionContext* libp2p_session_context_new() {
|
||||||
|
|
||||||
int libp2p_session_context_free(struct SessionContext* context) {
|
int libp2p_session_context_free(struct SessionContext* context) {
|
||||||
if (context != NULL) {
|
if (context != NULL) {
|
||||||
|
if (context->default_stream != NULL)
|
||||||
|
context->default_stream->close(context);
|
||||||
//TODO: Be more intelligent
|
//TODO: Be more intelligent
|
||||||
free(context);
|
free(context);
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,8 @@ struct Stream {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes a stream
|
* Closes a stream
|
||||||
|
*
|
||||||
|
* NOTE: This is also responsible for deallocating the Stream struct
|
||||||
* @param stream the stream context
|
* @param stream the stream context
|
||||||
* @returns true(1) on success, otherwise false(0)
|
* @returns true(1) on success, otherwise false(0)
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -20,11 +20,21 @@ int multistream_default_timeout = 5;
|
||||||
* An implementation of the libp2p multistream
|
* 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) {
|
int libp2p_net_multistream_close(void* stream_context) {
|
||||||
struct SessionContext* secure_context = (struct SessionContext*)stream_context;
|
struct SessionContext* secure_context = (struct SessionContext*)stream_context;
|
||||||
struct Stream* stream = secure_context->insecure_stream;
|
struct Stream* stream = secure_context->insecure_stream;
|
||||||
int socket_descriptor = *((int*)stream->socket_descriptor);
|
int socket_descriptor = *((int*)stream->socket_descriptor);
|
||||||
close(socket_descriptor);
|
close(socket_descriptor);
|
||||||
|
free(stream->socket_descriptor);
|
||||||
|
if (stream->address != NULL)
|
||||||
|
multiaddress_free(stream->address);
|
||||||
|
free(stream);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
56
peer/peer.c
56
peer/peer.c
|
@ -43,6 +43,36 @@ struct Libp2pPeer* libp2p_peer_new_from_multiaddress(const struct MultiAddress*
|
||||||
return out;
|
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
|
* Attempt to connect to the peer, setting connection_type correctly
|
||||||
* NOTE: If successful, this will set peer->connection to the stream
|
* 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
|
* Make a copy of a peer
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue