Search code examples
cvalgrind

Invalid read of size 8 - Valgrind in C


static boolean Ajout_element_config(dspList_t **p, char *mesfct, char *processus, char *requete_json, char *reponse_json, int actif)
{
    boolean erreur = false;
    char routine[]="Ajout_element_config";
   
    dspList_t *element = calloc(1,sizeof(dspList_t));
    
    if(element == NULL) 
    {
        /* Si l'allocation a échoué. */ 
        erreur = true;    
        SIItrace (1, CV_NIV_GRA_FATAL, routine, "Allocation a echoue");
        return -1;
    }

    element->mesfct = strdup(mesfct);
    element->processus = strdup(processus);
    element->requete_json = strdup(requete_json);
    element->reponse_json = strdup(reponse_json); 
    element->actif = actif;
    element->prec = *p;
    *p = element;       /* Le pointeur pointe sur le dernier élément. */
  
    return erreur;
}

static boolean Vide_config(dspList_t **p)
{
    boolean erreur = false;
    dspList_t *tmp;
    while (*p != NULL && tmp!= NULL)
    {
        free(tmp->mesfct);
        free(tmp->processus);
        free(tmp->requete_json);
        free(tmp->requete_json);  
        tmp = (*p)->prec;
        free(*p);
        *p = tmp;
    }
        
        erreur = false;
        return erreur;
    }

I received this porblem from valgrind :

Invalid read of size 8
at 0x4044AF: Vide_config (INTdspmng.sc:488)
by 0x403468: main (INTdspmng.sc:145)
Address 0x3ad6 is not stack'd, malloc'd or (recently) free'd

Solution

  • Finally, i found the solution :

    static boolean Vide_config(dspList_t **p)
    {
        boolean erreur = false;
        dspList_t *tmp = NULL;
        while (*p != NULL )
        {
            tmp = (*p)->prec;
            free((*p)->mesfct);
            free((*p)->processus);
            free((*p)->requete_json);
            free((*p)->reponse_json); 
            free(*p);
            *p = tmp;
        }   
        erreur = false;
        return erreur;
    }