commit c4cc33a67a2c2b70ff95abb8b41b24ca29c2ed0c
parent eadd9855ae9657076e01f3af4f412ffa6310bc85
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 15 Jan 2023 08:34:36 +0900
Fix memory leak.
Diffstat:
4 files changed, 13 insertions(+), 8 deletions(-)
diff --git a/ex9/list.c b/ex9/list.c
@@ -207,3 +207,11 @@ leqi(List *p, List *q, int (*eq)(void *, void*))
return 0;
}
+unsigned long int
+llen(List *p)
+{
+ unsigned long len = 0;
+ for (; p != NULL; p = p->next)
+ len++;
+ return len;
+}
diff --git a/ex9/list.h b/ex9/list.h
@@ -14,3 +14,4 @@ int lqsort(List *p, int (* cmp)(void *, void *));
void lswap(List *p, List *q);
int leqa(List *p, List *q);
int leqi(List *p, List *q, int (*eq)(void *, void *));
+unsigned long int llen(List *);
diff --git a/ex9/main.c b/ex9/main.c
@@ -317,9 +317,6 @@ game_play(void)
}
}
lfreei(ol, (void (*)(void *))&free_object);
- printf("obj_count: %d\n", get_obj_count());
- printf("lld_count: %d\n", get_lld_count());
- printf("list_count: %d\n", get_list_count());
}
void
diff --git a/ex9/object.c b/ex9/object.c
@@ -314,8 +314,7 @@ lhandle_collision(List *p)
l->o1 = (Object *)t->item;
l->dist = object_dist((Object *)s->item,
(Object *)t->item);
- tmplld = laddfront(lld, l);
- if (tmplld == NULL) {
+ if((tmplld = laddfront(lld, l)) == NULL){
fprintf(stderr, "lhandle_collision: Error. Could not"
"add item to lld\n");
exit(1);
@@ -331,16 +330,16 @@ lhandle_collision(List *p)
((LLD *)tmplld->item)->o1);
}
}
- lfreei(lld, &lldfree);
+ lfreei(lld, (void (*)(void *))&lldfree);
lfree(stack);
stack = NULL;
}
- tmp = laddfront(stack, p->item);
- if (tmp == NULL) {
+ if ((tmp = laddfront(stack, p->item)) == NULL) {
fprintf(stderr, "lhandle_collision: Error. add item to stack\n");
exit(1);
}
stack = tmp;
}
+ lfree(stack);
return;
}