Fixing memory leaks

This commit is contained in:
John Jones 2017-08-08 20:40:35 -05:00
parent 6754ba77b3
commit c58134db1c
7 changed files with 25 additions and 28 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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) {

View file

@ -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);

View file

@ -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) {

View file

@ -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);