From c58134db1c9a051982e4ccab5cf532da2e9dc028 Mon Sep 17 00:00:00 2001 From: John Jones Date: Tue, 8 Aug 2017 20:40:35 -0500 Subject: [PATCH] Fixing memory leaks --- cid/cid.c | 4 +++- core/ipfs_node.c | 6 +++--- exchange/bitswap/network.c | 20 +++++--------------- exchange/bitswap/peer_request_queue.c | 4 ++-- exchange/bitswap/wantlist_queue.c | 2 +- routing/online.c | 12 ++++++++---- test/exchange/test_bitswap.h | 5 +++-- 7 files changed, 25 insertions(+), 28 deletions(-) diff --git a/cid/cid.c b/cid/cid.c index b774771..01b362d 100644 --- a/cid/cid.c +++ b/cid/cid.c @@ -126,8 +126,10 @@ struct Cid* ipfs_cid_new(int version, const unsigned char* hash, size_t hash_len */ int ipfs_cid_free(struct Cid* cid) { if (cid != NULL) { - if (cid->hash != NULL) + if (cid->hash != NULL) { free(cid->hash); + cid->hash = NULL; + } free(cid); } return 1; diff --git a/core/ipfs_node.c b/core/ipfs_node.c index fd6669c..8bf9988 100644 --- a/core/ipfs_node.c +++ b/core/ipfs_node.c @@ -57,6 +57,9 @@ int ipfs_node_online_new(const char* repo_path, struct IpfsNode** node) { */ int ipfs_node_free(struct IpfsNode* node) { if (node != NULL) { + if (node->exchange != NULL) { + node->exchange->Close(node->exchange); + } if (node->providerstore != NULL) libp2p_providerstore_free(node->providerstore); if (node->peerstore != NULL) @@ -69,9 +72,6 @@ int ipfs_node_free(struct IpfsNode* node) { if (node->blockstore != NULL) { ipfs_blockstore_free(node->blockstore); } - if (node->exchange != NULL) { - node->exchange->Close(node->exchange); - } free(node); } return 1; diff --git a/exchange/bitswap/network.c b/exchange/bitswap/network.c index 61dcbcd..902b770 100644 --- a/exchange/bitswap/network.c +++ b/exchange/bitswap/network.c @@ -103,7 +103,8 @@ int ipfs_bitswap_network_handle_message(const struct IpfsNode* node, const struc if (message->payload != NULL) { for(int i = 0; i < message->payload->total; i++) { struct Block* blk = (struct Block*)libp2p_utils_vector_get(message->payload, i); - node->exchange->HasBlock(node->exchange, blk); + // we need a copy of the block so it survives the destruction of the message + node->exchange->HasBlock(node->exchange, ipfs_block_copy(blk)); } } // wantlist - what they want @@ -119,19 +120,8 @@ int ipfs_bitswap_network_handle_message(const struct IpfsNode* node, const struc ipfs_bitswap_message_free(message); return 0; } - // find the queue - struct PeerRequestEntry* queueEntry = ipfs_bitswap_peer_request_queue_find_entry(bitswapContext->peerRequestQueue, peer); - if (queueEntry == NULL) { - // add the queue - struct PeerRequest* peerRequest =ipfs_bitswap_peer_request_new(); - peerRequest->peer = peer; - ipfs_bitswap_peer_request_queue_add(bitswapContext->peerRequestQueue, peerRequest); - queueEntry = ipfs_bitswap_peer_request_queue_find_entry(bitswapContext->peerRequestQueue, peer); - if (queueEntry == NULL) { - ipfs_bitswap_message_free(message); - return 0; - } - } + // find the queue (adds it if it is not there) + struct PeerRequest* peerRequest = ipfs_peer_request_queue_find_peer(bitswapContext->peerRequestQueue, peer); for(int i = 0; i < message->wantlist->entries->total; i++) { struct WantlistEntry* entry = (struct WantlistEntry*) libp2p_utils_vector_get(message->wantlist->entries, i); // turn the "block" back into a cid @@ -142,7 +132,7 @@ int ipfs_bitswap_network_handle_message(const struct IpfsNode* node, const struc ipfs_bitswap_message_free(message); return 0; } - ipfs_bitswap_network_adjust_cid_queue(queueEntry->current->cids_they_want, cid, entry->cancel); + ipfs_bitswap_network_adjust_cid_queue(peerRequest->cids_they_want, cid, entry->cancel); } } ipfs_bitswap_message_free(message); diff --git a/exchange/bitswap/peer_request_queue.c b/exchange/bitswap/peer_request_queue.c index ddc4a2d..86d45e3 100644 --- a/exchange/bitswap/peer_request_queue.c +++ b/exchange/bitswap/peer_request_queue.c @@ -176,9 +176,9 @@ int ipfs_bitswap_peer_request_queue_remove(struct PeerRequestQueue* queue, struc } /** - * Finds a PeerRequestEntry that contains the specified PeerRequest + * Finds a PeerRequestEntry that contains the specified Peer * @param queue the queue to look through - * @param request what we're looking for + * @param peer what we're looking for * @returns the PeerRequestEntry or NULL if not found */ struct PeerRequestEntry* ipfs_bitswap_peer_request_queue_find_entry(struct PeerRequestQueue* queue, struct Libp2pPeer* peer) { diff --git a/exchange/bitswap/wantlist_queue.c b/exchange/bitswap/wantlist_queue.c index b1a74c0..2c79b76 100644 --- a/exchange/bitswap/wantlist_queue.c +++ b/exchange/bitswap/wantlist_queue.c @@ -262,7 +262,7 @@ int ipfs_bitswap_wantlist_get_block_remote(struct BitswapContext* context, struc // add this to their queue struct PeerRequest* queueEntry = ipfs_peer_request_queue_find_peer(context->peerRequestQueue, current); struct CidEntry* entry = ipfs_bitswap_peer_request_cid_entry_new(); - entry->cid = cid; + entry->cid = ipfs_cid_copy(cid); libp2p_utils_vector_add(queueEntry->cids_we_want, entry); // process this queue via bitswap protocol ipfs_bitswap_peer_request_process_entry(context, queueEntry); diff --git a/routing/online.c b/routing/online.c index 8cabad2..9af2475 100644 --- a/routing/online.c +++ b/routing/online.c @@ -97,9 +97,13 @@ int ipfs_routing_online_find_remote_providers(struct IpfsRouting* routing, const struct Libp2pPeer *current_peer = current_provider_peer_list_item->item; // if we can find the peer in the peerstore, use that one instead struct Libp2pPeer* peerstorePeer = libp2p_peerstore_get_peer(routing->local_node->peerstore, (unsigned char*)current_peer->id, current_peer->id_size); - if (peerstorePeer != NULL) - current_peer = peerstorePeer; - libp2p_utils_vector_add(*peers, libp2p_peer_copy(current_peer)); + if (peerstorePeer != NULL) { + // add it to the peerstore + libp2p_peerstore_add_peer(routing->local_node->peerstore, current_peer); + peerstorePeer = libp2p_peerstore_get_peer(routing->local_node->peerstore, (unsigned char*)current_peer->id, current_peer->id_size); + } + current_peer = peerstorePeer; + libp2p_utils_vector_add(*peers, current_peer); current_provider_peer_list_item = current_provider_peer_list_item->next; } libp2p_message_free(return_message); @@ -125,7 +129,7 @@ int ipfs_routing_online_find_remote_providers(struct IpfsRouting* routing, const * @param routing the context * @param key the hash to look for * @param key_size the size of the hash - * @param multiaddresses the results + * @param peers the results * @returns true(1) on success, otherwise false(0) */ int ipfs_routing_online_find_providers(struct IpfsRouting* routing, const unsigned char* key, size_t key_size, struct Libp2pVector** peers) { diff --git a/test/exchange/test_bitswap.h b/test/exchange/test_bitswap.h index e0d8c84..c5ebf97 100644 --- a/test/exchange/test_bitswap.h +++ b/test/exchange/test_bitswap.h @@ -343,8 +343,9 @@ int test_bitswap_retrieve_file_known_remote() { if (ma_vector2 != NULL) { libp2p_utils_vector_free(ma_vector2); } - if (result != NULL) - ipfs_block_free(result); + // this is freed by ipfs_node_free + //if (result != NULL) + // ipfs_block_free(result); if (cid != NULL) ipfs_cid_free(cid); ipfs_node_free(ipfs_node2);