C allocation - object was probably modified after being freed

3

I am doing a data structure program and am getting the following error:

  

arq (1966,0x7fff7970f300) malloc: * error for object 0x7fc058404c38:   incorrect checksum for freed object - object was not modified   after being freed.   * set a breakpoint in malloc_error_break to debug

But I'm not doing any free. The situation is this: I'm working with an AVL tree, the first time I try to insert an element into the tree, it works quietly.

Now, if I try to insert another element, either in the same tree or in another tree, it is the same record or the same one, points to this error, always in those addresses, I searched for several things but did not find the error. p>

I would like to know where the error is, as I can not imagine what to look for in the functions, since the first time it works normally but the second time it points to that error.

Part of the code:

 NODO *D;
  REG * R;
  int ok;

  inicializar(&D);
  R = criar_registro("GUT-2020","MCBA123","Carro","Azul",3,"Jose Joaquim", "1234","3214543");
  inserir(&D, "GUT-2020", *R, &ok);
  printf("Impressao: \n\n");
  exibir(D);
  //ate aqui tudo certo

  REG * R2;
  NODO * D2;
  int ok2;
  inicializar(&D2);
  R2 = criar_registro("GUT-2021","MCBA123","Carro","Azul",3,"Jose Joaquim", "1234","3214543");
  inserir(&D2, "GUT-2021", *R2, &ok2); //erro

functions involved in the problem:

void inicializar(NODO **Dic)
{
    *Dic = NULL;
}

REG * criar_registro(char placa[], char chassi[], char marca[], char modelo[], char portas, char proprietario[], char cpf[], char telefone[])
{
  REG * registro = (REG*)malloc(sizeof(REG));
  strcpy(registro->chave,placa);
  strcpy(registro->carro.placa,placa);
  strcpy(registro->carro.chassi,chassi);
  strcpy(registro->carro.marca,marca);
  strcpy(registro->carro.modelo,modelo);
  registro->carro.portas = portas;
  strcpy(registro->carro.proprietario,proprietario);
  strcpy(registro->carro.cpf,cpf);
  strcpy(registro->carro.telefone,telefone);

  return registro;
}

and the part that is executed in the insert function is as follows: The error happens the second time I'm going to run it, to a variable pointing to NULL (same thing happens the first time I run it, but the first time it works correctly)

  NODO * A

  if (*Dic == NULL)
    {                 //{INSERÇÃO}
      A = (NODO*) malloc(sizeof(Dic));
      if (A == NULL)
    return 0;
      A->esq = NULL;
      A->dir = NULL;
      strcpy(A->reg.chave, chave);
      A->reg.carro = reg.carro;
      A->FB = 0;
      *OK = 1;
      *Dic = A;
      return 1;
    }
    
asked by anonymous 23.01.2015 / 16:20

1 answer

3

I have the strong impression that the problem is sizeof(Dic) in allocating your last code. It will return the size of the pointer in the variable Dic and not the size of the structure (as intended). Thus, your variable manipulations (data copies) will invade areas of memory that you have not actually allocated.

Use sizeof(NODO) instead, which should resolve.

PS: You have not shared the code of the structures in the question, but analyzing very quickly it seems that your code has a lot of potential for memory invasion (in the copies of strings) and for memory leaks its insert function apparently copies the data to an internal structure, disregarding the pointer to the record that you had already allocated). I suggest giving a good proof in the code (if possible using strncpy to copy strings, and evaluating the process memory usage in some load tests). :)

    
23.01.2015 / 21:58