For what you have above you would actually leak regardless, because you copy the key twice (strdup( g_strdup(p1))
). Get rid of the outer strdup, so all you have is g_strdup(p1)
.
At that point, you would just need to g_free both the keys and values, which isn't hard to do (you just pass g_free to both the key_destroy_func and value_destroy_func arguments):
GTree* t = g_tree_new_full (key_compare_func, NULL, g_free, g_free);
However, since your values are integers, you can actually avoid the malloc by using GINT_TO_POINTER and GPOINTER_TO_INT. In that case, you would end up with something like this:
gint key_compare_func (gconstpointer a, gconstpointer b) {
return g_strcmp0 ((const char*) a, (const char*) b);
}
int readFilec(GTree *tree)
{
FILE * fp = fopen("cfg/InstrumentList_FULL.csv", "rb" );
char * line = NULL;
size_t len = 0;
ssize_t read;
GTree* t = g_tree_new_full (key_compare_func, NULL, g_free, NULL);
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
char *p1;
int p2;
printf("%s", line);
p1 = strtok(line, "|");
p2 = atoi(strtok(NULL, "|"));
g_tree_insert(tree, (gpointer) g_strdup(p1), GINT_TO_POINTER(p2));
//TrieAdd(&root, strdup(p1), p2);
printf("-%s%d ", p1, p2);
}
//exit(EXIT_SUCCESS);
}