0

I am trying to figure out what is wrong with my valgrind debugging. I am starting to learn valgrind and there are some things I do not know how to solve

==12902== HEAP SUMMARY:
==12902==     in use at exit: 40 bytes in 4 blocks
==12902==   total heap usage: 21 allocs, 17 frees, 2,400,792 bytes allocated
==12902== 
==12902== Searching for pointers to 4 not-freed blocks
==12902== Checked 77,768 bytes
==12902== 
==12902== 40 (16 direct, 24 indirect) bytes in 2 blocks are definitely  lost in loss record 2 of 2
==12902==    at 0x4C2AB80: malloc (in    /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12902==    by 0x400EAB: elequeue_ini (elequeue_point.c:15)
==12902==    by 0x400F90: elequeue_copy (elequeue_point.c:67)
==12902==    by 0x4012EC: queue_insert (queue.c:105)
==12902==    by 0x400C24: main (p3_e1.c:85)
==12902== 
==12902== LEAK SUMMARY:
==12902==    definitely lost: 16 bytes in 2 blocks
==12902==    indirectly lost: 24 bytes in 2 blocks
==12902==      possibly lost: 0 bytes in 0 blocks
==12902==    still reachable: 0 bytes in 0 blocks
==12902==         suppressed: 0 bytes in 0 blocks
==12902== 
==12902== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==12902== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Elequeue makes reference to the elements of a queue ADT. The codes involved are:

struct _EleQueue{
    Point *info;
};

EleQueue* elequeue_copy(const EleQueue * src){

    EleQueue *copied_ele;

    if(src==NULL){
        return NULL;
    }

    copied_ele=elequeue_ini();

    if(copied_ele==NULL){
        return NULL;
    }

    copied_ele->info=point_copy(src->info);

    return copied_ele;
}

EleQueue* elequeue_ini(){
    EleQueue *e;

    e=(EleQueue*)malloc(sizeof(EleQueue));
    if (e==NULL){
        return NULL;
    }
    e->info=NULL;
    return e;
}

Queue* queue_insert(Queue *q, const EleQueue* pElem){

    EleQueue *auxElem;

    if(!q || !pElem || queue_isFull(q) == TRUE){
         return NULL;
    }

    auxElem=elequeue_copy(pElem);

    if(auxElem == NULL){
        return NULL;
    }

    *(q->end) = auxElem;

    if(q->end == &(q->item[MAXQUEUE-1])){
        q->end = &(q->item[0]);
    } else {

    q -> end++;
    }

    return q;

}
  • 1
    Note: They say [you shouldn't cast the result of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Apr 14 '16 at 10:47
  • 1
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). For example, how are you using the posted functions? – MikeCAT Apr 14 '16 at 10:48
  • 2
    I don't see a single `free()`, are you releasing that memory in the end? – MoonBun Apr 14 '16 at 10:53

0 Answers0