Home > Software engineering >  memory leak in adding and deleting dynamic list
memory leak in adding and deleting dynamic list

Time:12-18

I really need your help in this matter. I have these two functions that do the following:

  1. copy face struct and add it to a list of faces (_face_list_entry_t)
  2. copy a pointer of struct and add it to a list of pointers (fib_entry_t )

I created these two functions to dynamically allocate and add new items (faces and pointers) to their respected list. (I don't want the function of removing item)

I am currently facing a memory leak and I am not sure if allocating/reallocating/freeing the memory in them causes this issue. Below is the the two functions:

int8_t face_list_add(_face_list_entry_t** face_list, int8_t* face_list_size, _face_list_entry_t* face)
{
    if (*face_list == NULL) {
        *face_list = malloc(sizeof(_face_list_entry_t));

        if (*face_list == NULL) {
            DEBUG("fail to allocate memory for face list\n");
            return -1;
        }

        *face_list_size = 1;
        (*face_list)[0].id   = face->id;
        (*face_list)[0].type = face->type;

        return 0;

    } else {
        // check for existing face entry
        for (int i = 0; i < *face_list_size;   i) {
            if ((*face_list)[i].id == face->id) {
                DEBUG("wildcard: same face exists in the fib entry\n");
                return 1;
            }
        }

        // need to add a new entry to the face list
        _face_list_entry_t *list = (_face_list_entry_t*) realloc( *face_list, (*face_list_size   1) * sizeof(_face_list_entry_t));
        DEBUG("facelist size = %d\n", (*face_list_size   1) * sizeof(_face_list_entry_t));
        if (list == NULL) {
            DEBUG("fail to reallocate memory for face list (size=%d)\n", *face_list_size);
            return -1;
        }
        *face_list = list;
        (*face_list)[*face_list_size].id = face->id;
        (*face_list)[*face_list_size].type = face->type;
          (*face_list_size);
        return 0;
    }
}

int8_t pointer_list_add(fib_entry_t ***fib_list, int8_t *fib_list_size, fib_entry_t **fib)
{
    if (*fib_list == NULL) {
        *fib_list = malloc(sizeof(fib_entry_t *));
        if (!*fib_list) {
            DEBUG("fail to allocate memory for fib list\n");
            return -1;
        }
        *fib_list_size = 1;
        **fib_list = *fib;
        return 0;
    } else {
        // check for existing fib entry
        for (int i = 0; i < *fib_list_size;   i) {
            if ((*fib_list)[i] == *fib) {
                DEBUG("same fib exists in the fib entry\n");
                return 1;
            }
        }

        // need to add a new entry to the fib list
        fib_entry_t **list = (fib_entry_t **)realloc(*fib_list, (*fib_list_size   1) * sizeof(fib_entry_t *));
        if (!list) {
            DEBUG("fail to reallocate memory for fib list (size=%d)\n", *fib_list_size);
            return -1;
        }
        **fib_list = *list;
        (*fib_list)[*fib_list_size] = *fib;
          (*fib_list_size);
        return 0;
    }
}

I call them like this:

res = face_list_add(&list_faces, &lst_faces_size, face);
res = pointer_list_add(&list_fibs, &list_fibs_size, &fib);

I delete the list like below. I don't want to delete the memory that each pointer is pointing to, I just want to delete the allocated memory for the list of pointers and the list of faces:

free(list_faces);
free(list_fibs);

Please let me know if I am doing it wrong or suggest a better way (low cost) as the device I am working on has a constrained capability, very low memory (256KB), and low process power, i.e. maintaining memory is crucial. Besides, usually, both lists are relatively small but in some cases can become bigger.

Your help is highly appreciated

CodePudding user response:

Given the face_list_add() and pointer_list_add() functions presented and these calls that (re)allocate memory ...

res = face_list_add(&list_faces, &lst_faces_size, face);
res = pointer_list_add(&list_fibs, &list_fibs_size, &fib);

, these free() calls ...

free(list_faces);
free(list_fibs);

... are correct for for freeing the memory allocated by those functions. Or they would be, if the functions were correct. face_list_add() appears to be ok, but there is an error in realloc branch of pointer_list_add(), here:

        **fib_list = *list;

That should be

        *fib_list = list;

, analogous to

        *fib_list = malloc(sizeof(fib_entry_t *));

in the initial allocation branch.

  • Related