Domanda

EDIT

I create gtree like that:

GTree* t = g_tree_new_full((GCompareDataFunc)g_ascii_strcasecmp,NULL,free_data,free_data);

I have free function:

void free_data (gpointer data)
{
  free (data);
}

I insert stuff like this:

int HoleInstrumentenListe(GTree *tree)
{
  printf("HoleInstrumentenListe\n");
  FILE * fp = fopen("cfg/InstrumentList_FULL.csv", "rb" );
  char * line = NULL;
  size_t len = 0;
  ssize_t read;
  int *pQotStatus;

  if (fp == NULL) {
    printf("Konnte Instrumentenliste nicht laden\n");
    exit(FAILLoadInstrumentList);
  }

  while ((read = getline(&line, &len, fp)) != -1)
  {
    char *p1;
    int  *p2 = malloc(sizeof (int));

    p1 = strtok(line, "|");
    *p2 = atoi(strtok(NULL, "|"));

    if ((pQotStatus = (int *) g_tree_lookup(tree, p1)) != NULL )
    {
       *p2 = (*pQotStatus | (1<<(*p2-1)));
    }
    else {
       *p2 = (1<<(*p2-1));
    }
    g_tree_insert(tree, (gpointer) g_strdup(p1), (gpointer)p2);
  }
  fclose(fp);
  return 1;
}

I destroy tree like that:

g_tree_destroy (t);

I still have memory leaks.

Some valgrind output:

==4828== 1,024 bytes in 1 blocks are still reachable in loss record 4 of 260
==4828==    at 0x4A21370: malloc (vg_replace_malloc.c:291)
==4828==    by 0x4B5AA95: g_malloc (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B5BC59: g_mem_chunk_alloc (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B733D5: ??? (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B73609: ??? (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B735D6: ??? (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B735D6: ??? (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B73511: ??? (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B73511: ??? (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x4B736BA: g_tree_insert (in /opt/gnome/lib64/libglib-2.0.so.0.800.6)
==4828==    by 0x401037: HoleInstrumentenListe (unzipper_m.c:101)
==4828==    by 0x401879: main (unzipper_m.c:396)

Line 101 is

g_tree_insert(tree, (gpointer) g_strdup(p1), (gpointer)p2);
È stato utile?

Soluzione

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);

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top