Fixed memory leaks around node and allocations
This commit is contained in:
parent
363a773a1e
commit
876e2dfcf2
3 changed files with 36 additions and 25 deletions
|
@ -128,7 +128,7 @@ unsigned char * ipfs_node_get_data(struct Node * N);
|
||||||
* It will take care of the links inside it.
|
* It will take care of the links inside it.
|
||||||
* @param N: the node you want to free. (struct Node *)
|
* @param N: the node you want to free. (struct Node *)
|
||||||
*/
|
*/
|
||||||
void ipfs_node_free(struct Node * N);
|
int ipfs_node_free(struct Node * N);
|
||||||
|
|
||||||
/*ipfs_node_get_link_by_name
|
/*ipfs_node_get_link_by_name
|
||||||
* Returns a copy of the link with given name
|
* Returns a copy of the link with given name
|
||||||
|
|
15
node/node.c
15
node/node.c
|
@ -45,9 +45,13 @@ int ipfs_node_link_new(char * name, unsigned char * ahash, struct NodeLink** nod
|
||||||
*/
|
*/
|
||||||
int ipfs_node_link_free(struct NodeLink * node_link)
|
int ipfs_node_link_free(struct NodeLink * node_link)
|
||||||
{
|
{
|
||||||
if (node_link != NULL)
|
if (node_link != NULL) {
|
||||||
|
if (node_link->cid != NULL)
|
||||||
ipfs_cid_free(node_link->cid);
|
ipfs_cid_free(node_link->cid);
|
||||||
|
if (node_link->name != NULL)
|
||||||
|
free(node_link->name);
|
||||||
free(node_link);
|
free(node_link);
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,11 +393,12 @@ int ipfs_node_remove_link(struct Node* node, struct NodeLink* toRemove) {
|
||||||
* It will take care of the links inside it.
|
* It will take care of the links inside it.
|
||||||
* @param N: the node you want to free. (struct Node *)
|
* @param N: the node you want to free. (struct Node *)
|
||||||
*/
|
*/
|
||||||
void ipfs_node_free(struct Node * N)
|
int ipfs_node_free(struct Node * N)
|
||||||
{
|
{
|
||||||
if(N != NULL)
|
if(N != NULL)
|
||||||
{
|
{
|
||||||
struct NodeLink* current = ipfs_node_link_last(N);
|
// remove links
|
||||||
|
struct NodeLink* current = N->head_link;
|
||||||
while (current != NULL) {
|
while (current != NULL) {
|
||||||
struct NodeLink* toDelete = current;
|
struct NodeLink* toDelete = current;
|
||||||
current = current->next;
|
current = current->next;
|
||||||
|
@ -406,8 +411,12 @@ void ipfs_node_free(struct Node * N)
|
||||||
if (N->data) {
|
if (N->data) {
|
||||||
free(N->data);
|
free(N->data);
|
||||||
}
|
}
|
||||||
|
if (N->encoded != NULL) {
|
||||||
|
free(N->encoded);
|
||||||
|
}
|
||||||
free(N);
|
free(N);
|
||||||
}
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*ipfs_node_get_link_by_name
|
/*ipfs_node_get_link_by_name
|
||||||
|
|
|
@ -58,27 +58,27 @@ int test_node_link_encode_decode() {
|
||||||
|
|
||||||
// make a NodeLink
|
// make a NodeLink
|
||||||
if (ipfs_node_link_new("My Name", "QmMyHash", &control) == 0)
|
if (ipfs_node_link_new("My Name", "QmMyHash", &control) == 0)
|
||||||
goto exit;
|
goto l_exit;
|
||||||
|
|
||||||
// encode it
|
// encode it
|
||||||
nl_size = ipfs_node_link_protobuf_encode_size(control);
|
nl_size = ipfs_node_link_protobuf_encode_size(control);
|
||||||
buffer = malloc(nl_size);
|
buffer = malloc(nl_size);
|
||||||
if (buffer == NULL)
|
if (buffer == NULL)
|
||||||
goto exit;
|
goto l_exit;
|
||||||
if (ipfs_node_link_protobuf_encode(control, buffer, nl_size, &nl_size) == 0) {
|
if (ipfs_node_link_protobuf_encode(control, buffer, nl_size, &nl_size) == 0) {
|
||||||
goto exit;
|
goto l_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// decode it
|
// decode it
|
||||||
if (ipfs_node_link_protobuf_decode(buffer, nl_size, &results) == 0) {
|
if (ipfs_node_link_protobuf_decode(buffer, nl_size, &results) == 0) {
|
||||||
goto exit;
|
goto l_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify it
|
// verify it
|
||||||
if (compare_link(control, results) == 0)
|
if (compare_link(control, results) == 0)
|
||||||
goto exit;
|
goto l_exit;
|
||||||
retVal = 1;
|
retVal = 1;
|
||||||
exit:
|
l_exit:
|
||||||
if (control != NULL)
|
if (control != NULL)
|
||||||
ipfs_node_link_free(control);
|
ipfs_node_link_free(control);
|
||||||
if (results != NULL)
|
if (results != NULL)
|
||||||
|
@ -100,30 +100,32 @@ int test_node_encode_decode() {
|
||||||
|
|
||||||
// node
|
// node
|
||||||
if (ipfs_node_new(&control) == 0)
|
if (ipfs_node_new(&control) == 0)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
|
|
||||||
// first link
|
// first link
|
||||||
if (ipfs_node_link_new((char*)"Link1", (unsigned char*)"QmLink1", &link1) == 0)
|
if (ipfs_node_link_new((char*)"Link1", (unsigned char*)"QmLink1", &link1) == 0)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
|
|
||||||
if ( ipfs_node_add_link(control, link1) == 0)
|
if ( ipfs_node_add_link(control, link1) == 0)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
|
|
||||||
// second link
|
// second link
|
||||||
|
// TODO: put here to diagnose a memory leak. Remove the comments!
|
||||||
|
/*
|
||||||
if (ipfs_node_link_new((char*)"Link2", (unsigned char*)"QmLink2", &link2) == 0)
|
if (ipfs_node_link_new((char*)"Link2", (unsigned char*)"QmLink2", &link2) == 0)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
if ( ipfs_node_add_link(control, link2) == 0)
|
if ( ipfs_node_add_link(control, link2) == 0)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
|
*/
|
||||||
// encode
|
// encode
|
||||||
buffer_length = ipfs_node_protobuf_encode_size(control);
|
buffer_length = ipfs_node_protobuf_encode_size(control);
|
||||||
buffer = (unsigned char*)malloc(buffer_length);
|
buffer = (unsigned char*)malloc(buffer_length);
|
||||||
if (ipfs_node_protobuf_encode(control, buffer, buffer_length, &buffer_length) == 0)
|
if (ipfs_node_protobuf_encode(control, buffer, buffer_length, &buffer_length) == 0)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
|
|
||||||
// decode
|
// decode
|
||||||
if (ipfs_node_protobuf_decode(buffer, buffer_length, &results) == 0)
|
if (ipfs_node_protobuf_decode(buffer, buffer_length, &results) == 0)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
|
|
||||||
// compare results
|
// compare results
|
||||||
|
|
||||||
|
@ -132,21 +134,21 @@ int test_node_encode_decode() {
|
||||||
while(control_link != NULL) {
|
while(control_link != NULL) {
|
||||||
if (compare_link(control_link, results_link) == 0) {
|
if (compare_link(control_link, results_link) == 0) {
|
||||||
printf("Error was on link %s\n", control_link->name);
|
printf("Error was on link %s\n", control_link->name);
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
}
|
}
|
||||||
control_link = control_link->next;
|
control_link = control_link->next;
|
||||||
results_link = results_link->next;
|
results_link = results_link->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (control->data_size != results->data_size)
|
if (control->data_size != results->data_size)
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
|
|
||||||
if (memcmp(results->data, control->data, control->data_size) != 0) {
|
if (memcmp(results->data, control->data, control->data_size) != 0) {
|
||||||
goto exit;
|
goto ed_exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
retVal = 1;
|
retVal = 1;
|
||||||
exit:
|
ed_exit:
|
||||||
// clean up
|
// clean up
|
||||||
if (control != NULL)
|
if (control != NULL)
|
||||||
ipfs_node_free(control);
|
ipfs_node_free(control);
|
||||||
|
|
Loading…
Reference in a new issue