Welcome to the Builder Academy

Question Memory leak - I am stumped

More
08 Jun 2016 21:52 - 09 Jun 2016 22:06 #5984 by cunning
I have a memory leak that valgrind is pointing out. I cannot solve the two listed. This is from DOOM code.

Attachment code-2.pdf not found



Code:
#define NUM_OBJ_UNIQUE_POOLS 5000 struct obj_unique_hash_elem **obj_unique_hash_pools = NULL; void init_obj_unique_hash() { int i; 787-> CREATE(obj_unique_hash_pools, struct obj_unique_hash_elem *, NUM_OBJ_UNIQUE_POOLS); for (i = 0; i < NUM_OBJ_UNIQUE_POOLS; i++) { obj_unique_hash_pools[i] = NULL; } }
Code:
void add_unique_id(struct obj_data *obj) { struct obj_unique_hash_elem *elem = NULL; int i; if (!obj_unique_hash_pools) 4158-> init_obj_unique_hash(); if (obj->unique_id == -1) { if (sizeof(long long) > sizeof(long)) obj->unique_id = (((long long)circle_random()) << (sizeof(long long) * 4)) + circle_random(); else obj->unique_id = circle_random(); } if (CONFIG_ALL_ITEMS_UNIQUE) { if (!IS_SET_AR(GET_OBJ_EXTRA(obj), ITEM_UNIQUE_SAVE)) SET_BIT_AR(GET_OBJ_EXTRA(obj), ITEM_UNIQUE_SAVE); } 4174-> CREATE(elem, struct obj_unique_hash_elem, 1); elem->generation = obj->generation; elem->unique_id = obj->unique_id; elem->obj = obj; i = obj->unique_id % NUM_OBJ_UNIQUE_POOLS; elem->next_e = obj_unique_hash_pools[i]; obj_unique_hash_pools[i] = elem; }
Valgrind output:
==16869== 64 bytes in 2 blocks are indirectly lost in loss record 11 of 40
==16869== at 0x4C2AD10: calloc (vg_replace_malloc.c:623)
==16869== by 0x57090F: add_unique_id (db.c:4174)
==16869== by 0x544362: init_train (metro.c:248)
==16869== by 0x57CF21: boot_db (db.c:982)
==16869== by 0x40351E: init_game (comm.c:551)
==16869== by 0x40351E: main (comm.c:381)

==16869== 40,160 (40,000 direct, 160 indirect) bytes in 1 blocks are definitely lost in loss record 39 of 40
==16869== at 0x4C2AD10: calloc (vg_replace_malloc.c:623)
==16869== by 0x56E7F2: init_obj_unique_hash (db.c:787)
==16869== by 0x57098E: add_unique_id (db.c:4158)
==16869== by 0x544585: init_train (metro.c:193)
==16869== by 0x57CF21: boot_db (db.c:982)
==16869== by 0x40351E: init_game (comm.c:551)
==16869== by 0x40351E: main (comm.c:381)
Attachments:
Last edit: 09 Jun 2016 22:06 by cunning.

Please Log in or Create an account to join the conversation.

More
09 Jun 2016 20:29 #5989 by thomas
Replied by thomas on topic Memory leak - I am stumped
The pool is obviously supposed to live until the system shuts down.

Is there any code anywhere to make sure it is cleared?

Please Log in or Create an account to join the conversation.

More
09 Jun 2016 20:48 - 09 Jun 2016 22:03 #5990 by cunning
Replied by cunning on topic Memory leak - I am stumped

thomas wrote: The pool is obviously supposed to live until the system shuts down.

Is there any code anywhere to make sure it is cleared?




Attachment code2.pdf not found




Yes,
Code:
struct obj_unique_hash_elem { time_t generation; long long unique_id; struct obj_data *obj; struct obj_unique_hash_elem *next_e; };
Code:
void free_obj_unique_hash() { int i; struct obj_unique_hash_elem *elem; struct obj_unique_hash_elem *next_elem; if (obj_unique_hash_pools) { for (i = 0; i < NUM_OBJ_UNIQUE_POOLS; i++) { elem = obj_unique_hash_pools[i]; while (elem) { next_elem = elem->next_e; free(elem); elem = next_elem; } } free(obj_unique_hash_pools); } }

free_obj_unique_hash(); is called within destroy_db().
Attachments:
Last edit: 09 Jun 2016 22:03 by cunning.

Please Log in or Create an account to join the conversation.

More
10 Jun 2016 23:13 #5997 by thomas
Replied by thomas on topic Memory leak - I am stumped
I think this is either a false positive or you're not triggering the free_obj_unique_hash() function. I'd add a log there to make sure.

Please Log in or Create an account to join the conversation.

More
17 Jun 2016 00:18 #6015 by cunning
Replied by cunning on topic Memory leak - I am stumped
I think your right, I have a few others like that and I was positive it was false. STRDUP's are hard suckers to account for.

Please Log in or Create an account to join the conversation.

Time to create page: 0.203 seconds