Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leaks and other things in 29/btree.c #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions 29/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ static void put(btree_t *btree, char *key, char *val) {

// need to split root
node_t *t = initNode(2);
free(t->children[0]);
free(t->children[1]);
t->children[0] = initEntry(btree->root->children[0]->key, NULL, btree->root);
t->children[1] = initEntry(u->children[0]->key, NULL, u);
btree->root = t;
Expand All @@ -145,15 +147,20 @@ static node_t *insert(node_t *h, char *key, char *val, int ht) {
for (j = 0; j < h->m; j++) {
if ((j + 1 == h->m) || strcmp(key, h->children[j + 1]->key) < 0) {
node_t *u = insert(h->children[j++]->next, key, val, ht - 1);
if (u == NULL)
if (u == NULL){
// important to add this and then avoid subsequent chained mem leaks.
free(t);
return NULL;
}
t->key = u->children[0]->key;
t->next = u;
break;
}
}
}

// this also will cause chained mem leaks.
free(h->children[h->m]);
for (int i = h->m; i > j; i--)
h->children[i] = h->children[i - 1];
h->children[j] = t;
Expand All @@ -168,8 +175,12 @@ static node_t *insert(node_t *h, char *key, char *val, int ht) {
static node_t *split(node_t *h) {
node_t *t = initNode(M / 2);
h->m = M / 2;
for (int j = 0; j < M / 2; j++)
for (int j = 0; j < M / 2; j++){
free(t->children[j]);
t->children[j] = h->children[M / 2 + j];
// avoid double free.
h->children[M / 2 + j]=NULL;
}
return t;
}

Expand All @@ -179,7 +190,11 @@ static char *toString(btree_t *btree) {
}

static char *toStringHelper(node_t *h, int ht, char *indent) {
char *s = malloc(1024 * sizeof(char));
// avoid weird characters and subsequent weird behavior when free by explicit initialization.
char *s = calloc(1024 * sizeof(char),1024);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this line should be calloc(sizeof(char), 1024)?. I'll review the rest code later, I need some time to refamiliarize the code and data structure...

Copy link
Contributor Author

@sci-42ver sci-42ver Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my mistake.

if (!s) {
handle_error_en(errno, "calloc s");
}
entry_t *children[M];
for (size_t i = 0; i < M; i++) {
children[i] = h->children[i];
Expand All @@ -199,16 +214,17 @@ static char *toStringHelper(node_t *h, int ht, char *indent) {
}
} else {
for (int j = 0; j < h->m; j++) {
if (j > 0) {
strcat(s, indent);
strcat(s, "(");
strcat(s, children[j]->key);
strcat(s, ")\n");
}
strcat(s, indent);
strcat(s, "(");
strcat(s, children[j]->key);
strcat(s, ")\n");
char *intentCp = malloc(1024 * sizeof(char));
strcpy(intentCp, indent);
strcat(intentCp, " ");
strcat(s, toStringHelper(children[j]->next, ht - 1, intentCp));
char * subtree_str = toStringHelper(children[j]->next, ht - 1, intentCp);
strcat(s, subtree_str);
free(intentCp);
free(subtree_str);
}
}
return s;
Expand All @@ -227,6 +243,22 @@ static void *thread_function(void *args) {
pthread_exit(EXIT_SUCCESS);
}

void free_node (node_t* subnode){
// recommend use `i<M` to free all `initEntry` allocated memory.
for (int i=0; i<M; i++) {
if (subnode->children[i]==NULL) {
continue;
}
node_t* target_node = subnode->children[i]->next;
if (target_node!=NULL) {
free_node(target_node);
}
free(subnode->children[i]);
}
free(subnode);
return;
}

// Unit tests the BTree data type.
int main(int argc, char *argv[]) {
for (int i = 1; i < 11; i++) {
Expand Down Expand Up @@ -256,6 +288,11 @@ int main(int argc, char *argv[]) {
printf("Time (seconds): %f\n\n",
((double)(endusec - startusec) / ONE_MILLION));
printf("size: %d\n\n", btree->n);
char *btree_structure = toString(btree);
printf("%s\n", btree_structure);
free(btree_structure);
free_node(btree->root);
free(btree);
free(threads);
}

Expand Down Expand Up @@ -287,6 +324,5 @@ int main(int argc, char *argv[]) {

// printf("size: %d\n", btree->n);
// printf("height: %d\n", btree->height);
// printf("%s\n", toString(btree));
return 0;
}