Fix of memory leaks

This commit is contained in:
John Jones 2017-08-03 17:46:20 -05:00
parent 2b24b00324
commit 3cc75058f0
7 changed files with 87 additions and 13 deletions

View file

@ -59,6 +59,14 @@ int ipfs_bitswap_free(struct Exchange* exchange) {
struct BitswapContext* bitswapContext = (struct BitswapContext*) exchange->exchangeContext;
if (bitswapContext != NULL)
ipfs_bitswap_engine_stop(bitswapContext);
if (bitswapContext->localWantlist != NULL) {
ipfs_bitswap_wantlist_queue_free(bitswapContext->localWantlist);
bitswapContext->localWantlist = NULL;
}
if (bitswapContext->peerRequestQueue != NULL) {
ipfs_bitswap_peer_request_queue_free(bitswapContext->peerRequestQueue);
bitswapContext->peerRequestQueue = NULL;
}
free(exchange->exchangeContext);
}
free(exchange);

View file

@ -97,6 +97,7 @@ void* ipfs_bitswap_engine_peer_request_processor_start(void* ctx) {
if (current_peer_entry->sessionContext->default_stream->read(current_peer_entry->sessionContext, &buffer, &buffer_len, 1)) {
// handle it
int retVal = ipfs_multistream_marshal(buffer, buffer_len, current_peer_entry->sessionContext, context->ipfsNode);
free(buffer);
did_some_processing = 1;
if (retVal == -1) {
// there was a problem. Clean up

View file

@ -139,10 +139,12 @@ int ipfs_bitswap_network_handle_message(const struct IpfsNode* node, const struc
if (!ipfs_cid_protobuf_decode(entry->block, entry->block_size, &cid) || cid->hash_length == 0) {
libp2p_logger_error("bitswap_network", "Message had invalid CID\n");
ipfs_cid_free(cid);
ipfs_bitswap_message_free(message);
return 0;
}
ipfs_bitswap_network_adjust_cid_queue(queueEntry->current->cids_they_want, cid, entry->cancel);
}
}
ipfs_bitswap_message_free(message);
return 1;
}

View file

@ -57,13 +57,45 @@ struct PeerRequest* ipfs_bitswap_peer_request_new() {
return request;
}
int ipfs_bitswap_cid_entry_free(struct CidEntry* entry) {
if (entry != NULL) {
if (entry->cid != NULL) {
ipfs_cid_free(entry->cid);
entry->cid = NULL;
}
free(entry);
}
return 1;
}
/**
* Free resources from a PeerRequest
* @param request the request to free
* @returns true(1)
*/
int ipfs_bitswap_peer_request_free(struct PeerRequest* request) {
free(request);
if (request != NULL) {
for(int i = 0; i < request->cids_we_want->total; i++) {
struct CidEntry* entry = (struct CidEntry*)libp2p_utils_vector_get(request->cids_we_want, i);
ipfs_bitswap_cid_entry_free(entry);
}
libp2p_utils_vector_free(request->cids_we_want);
request->cids_we_want = NULL;
for(int i = 0; i < request->cids_they_want->total; i++) {
struct CidEntry* entry = (struct CidEntry*)libp2p_utils_vector_get(request->cids_they_want, i);
ipfs_bitswap_cid_entry_free(entry);
}
libp2p_utils_vector_free(request->cids_they_want);
request->cids_they_want = NULL;
for(int i = 0; i < request->blocks_we_want_to_send->total; i++) {
struct Block* block = (struct Block*)libp2p_utils_vector_get(request->blocks_we_want_to_send, i);
ipfs_block_free(block);
}
libp2p_utils_vector_free(request->blocks_we_want_to_send);
request->blocks_we_want_to_send = NULL;
free(request);
}
return 1;
}
@ -341,8 +373,11 @@ int ipfs_bitswap_peer_request_process_entry(const struct BitswapContext* context
// add requests that we would like
ipfs_bitswap_message_add_wantlist_items(msg, request->cids_we_want);
// send message
if (ipfs_bitswap_network_send_message(context, request->peer, msg))
if (ipfs_bitswap_network_send_message(context, request->peer, msg)) {
ipfs_bitswap_message_free(msg);
return 1;
}
ipfs_bitswap_message_free(msg);
}
}
return 0;

View file

@ -47,6 +47,10 @@ struct WantListQueue* ipfs_bitswap_wantlist_queue_new() {
int ipfs_bitswap_wantlist_queue_free(struct WantListQueue* wantlist) {
if (wantlist != NULL) {
if (wantlist->queue != NULL) {
for(int i = 0; i < wantlist->queue->total; i++) {
struct WantListQueueEntry* entry = (struct WantListQueueEntry*)libp2p_utils_vector_get(wantlist->queue, i);
ipfs_bitswap_wantlist_queue_entry_free(entry);
}
libp2p_utils_vector_free(wantlist->queue);
wantlist->queue = NULL;
}
@ -262,7 +266,9 @@ int ipfs_bitswap_wantlist_get_block_remote(struct BitswapContext* context, struc
libp2p_utils_vector_add(queueEntry->cids_we_want, entry);
// process this queue via bitswap protocol
ipfs_bitswap_peer_request_process_entry(context, queueEntry);
//libp2p_peer_free(current);
}
libp2p_utils_vector_free(providers);
return 1;
}
return 0;