Fixing memory leak

yamux
John Jones 2017-08-08 20:40:06 -05:00
parent a8f4925bbe
commit 1a45924151
2 changed files with 41 additions and 1 deletions

View File

@ -1,6 +1,7 @@
#include <stdlib.h>
#include <string.h>
#include "multiaddr/multiaddr.h"
#include "libp2p/crypto/ephemeral.h"
#include "libp2p/conn/session.h"
#include "libp2p/net/stream.h"
@ -47,6 +48,42 @@ int libp2p_session_context_free(struct SessionContext* context) {
context->default_stream = NULL;
context->insecure_stream = NULL;
context->secure_stream = NULL;
if (context->chosen_cipher != NULL) {
free(context->chosen_cipher);
context->chosen_cipher = NULL;
}
if (context->chosen_curve != NULL) {
free(context->chosen_curve);
context->chosen_curve = NULL;
}
if (context->chosen_hash != NULL) {
free(context->chosen_hash);
context->chosen_hash = NULL;
}
if (context->shared_key != NULL) {
free(context->shared_key);
context->shared_key = NULL;
}
if (context->remote_peer_id != NULL) {
free(context->remote_peer_id);
context->remote_peer_id = NULL;
}
if (context->remote_ephemeral_public_key != NULL) {
free(context->remote_ephemeral_public_key);
context->remote_ephemeral_public_key = NULL;
}
if (context->local_stretched_key != NULL) {
libp2p_crypto_ephemeral_stretched_key_free(context->local_stretched_key);
context->local_stretched_key = NULL;
}
if (context->remote_stretched_key != NULL) {
libp2p_crypto_ephemeral_stretched_key_free(context->remote_stretched_key);
context->remote_stretched_key = NULL;
}
if (context->ephemeral_private_key != NULL) {
libp2p_crypto_ephemeral_key_free(context->ephemeral_private_key);
context->ephemeral_private_key = NULL;
}
free(context);
}
return 1;

View File

@ -69,13 +69,16 @@ int libp2p_peerstore_free(struct Peerstore* in) {
if (in != NULL) {
struct Libp2pLinkedList* current = in->head_entry;
struct Libp2pLinkedList* next = NULL;
// first empty out the peer entries
while (current != NULL) {
next = current->next;
libp2p_peer_entry_free((struct PeerEntry*)current->item);
current->item = NULL;
libp2p_utils_linked_list_free(current);
current = next;
}
// now free the linked list entries
libp2p_utils_linked_list_free(in->head_entry);
// and finally the peerstore itself
free(in);
}
return 1;