-2

I was clearing memory from previous mallocs and I got this error : Error in `./prot': double free or corruption (fasttop) . I allocated memory for some structs and I got this error when trying to free them. This are the structs I used:

struct ProdutoN
{
    char prod[MAXBUFFPROD];
    int altura;
    struct ProdutoN *esq;
    struct ProdutoN *dir;
};


struct ArrayProd
{
    ProdutoNP lista[26];
    int tamanho[26];
};

I created an incomplete type called CatalogoProdutos that points to the struct called ArrayProd. Inside that same struct I created an incomplete type called ProdutoNP that points to the struct called ProdutoN (which is an avl tree). This is how I have iniciated this structs :

void inicializa(CatalogoProdutos l)
{
    int i;
    l=(CatalogoProdutos)malloc(sizeof(struct ArrayProd));
    for(i=0;i<26;i++)
    {

        l->lista[i] = (ProdutoNP)malloc(sizeof(struct ProdutoN));
        l->lista[i] = NULL;
        l->tamanho[i]=0;

    }

}

This is how I tried to free this :

void freePNP (ProdutoNP lista)
{
    if (lista == NULL)
    return;

    free (lista->prod);
    freePNP (lista->esq);
    freePNP (lista->dir);

    free(lista);
}
void freeCP (CatalogoProdutos l)
{
    int i;

    if (l == NULL)
    return; 

    for (i = 0 ; i < 26 ; i++)
    freePNP(l->lista[i]);

    free(l);
}

Definitions of the incomplete types:

typedef struct ProdutoN *ProdutoNP;
typedef struct ArrayProd *CatalogoProdutos;

What I am doing wrong ?

José Cunha
  • 107
  • 1
  • 9
  • 1
    `l->lista[i] = (ProdutoNP)malloc(sizeof(struct ProdutoN)); l->lista[i] = NULL;` what is that suppossed to mean? – Sourav Ghosh Apr 20 '16 at 21:28
  • 2
    It would be useful to post the definitions of `ProdutoNP` and `CatalogoProdutos`. – tinman Apr 20 '16 at 21:28
  • 1
    @SouravGhosh i think he just wants to leak memory there :) – ddz Apr 20 '16 at 21:29
  • 1
    1) Don't cast the result of `malloc`. 2) As I suspect the missing types are `typedef`ed pointers: Don't `typedef` pointers! This obfuscates the special semantics and eventually results in confusion. – too honest for this site Apr 20 '16 at 21:30
  • `l=(CatalogoProdutos)malloc(sizeof(struct ArrayProd));` can't set to caller side variable. Because `l` is local variable. – BLUEPIXY Apr 20 '16 at 21:39
  • There are lots of problems with this code. `inicializa` is calling `malloc`, then immediately overwriting the pointers that were returned with `NULL`, losing them forever. At best that's a memory leak. `freePNP` is attempting to free `prod`, which is an array that is allocated as part of the containing structure. This makes no sense. Hint: The calls to `free` should be in one-to-one correspondence with the calls to `malloc`. – Tom Karzes Apr 20 '16 at 21:40
  • Possible duplicate of [How to track down a "double free or corruption" error](https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error) – Raedwald Dec 06 '18 at 13:39

1 Answers1

2

You are trying to free the member of the struct that is not dynamically allocated.

free (lista->prod);
tinman
  • 6,348
  • 1
  • 30
  • 43