Dynamic memory and realloc(), freeing memory
I've made a registry editor with a treeview for the keys and a listview for the values.
I use a global pointer to a structure to hold the list of values:
// in registry.h
When a key is loaded, a function finds the total number of values, and allocates memory for the global structure like so:
// in registry.c
struct value_t *g_values;
I loaded task manager to watch the amount of memory consumption.
memset(g_values, 0, sizeof(g_values));
g_values = (struct value_t*)realloc(g_values,
(data.cValues + 32) * sizeof(struct value_t) + sizeof(struct value_t));
// fill structure members...
// the char* data member is reallocated in a similar way as above
With each click of a different registry key, realloc() is called with the global structure and memory is allocated for the number of values in that key.
I would expect the memory to reduce if I load a key with less or no values, since realloc() calls free(). But when loading a blank key, the memory remains the same, and then upon loading a key with many values, the memory increases, and keeps increasing, eventually reaching a ridiculous amount.
Why is the memory not being free'd? Or is task manager a unreliable tool to monitor dynamic memory?
Edit:After carefully re-reading this http://www.cplusplus.com/reference/c...tdlib/realloc/,
I found that calling memset(&g_values, 0, sizeof(g_values)) was making it a NULL pointer, and thus realloc() was allocating a new block each time. My mistake.