Allocation in C - Object was probably modified after being Freed

Asked

Viewed 92 times

3

I am making 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 probably 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 am working with an AVL tree, the first time I try to insert an element in the tree, it works smoothly.

Now, if I try to insert another element, either in the same tree or another tree, the same record or the same, points out this error, always at these addresses, I searched several things but did not find the error.

I would like to know where the error is, because I can’t imagine what to look for in the functions, since the first time it works normally but the second time it points out 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 function insert is as follows: The error happens the second time I run it, for a variable pointing to NULL (same as the first time I run it, but this first time 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;
    }
  • 1

    The problem may be in the functions. Post their code, if possible.

  • posted the code of the functions that are executed!

1 answer

3


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

Use sizeof(NODO) in place, that must solve.

P.S.: You didn’t share the code of the structures in the question, but looking at it very quickly it seems that your code has a lot of potential for memory invasion (in the copies of the strings) and for memory leaks (in the creation of the records, since its function insert apparently copies the data to an internal structure, disregarding the pointer to the record you had already allocated). I suggest a good review of the code (if possible using strncpy to copy strings, and evaluating process memory usage in some load tests). :)

  • 1

    That was the problem, it was a distraction, I do not believe I lost so much time so hahaha - but thank you for the help and I will check the code yes, I will take this tip into consideration!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.