'Strange behavior on function argument in C

I have a problem with a program running in an ARM4 MCU. Debugging the code I found this situation: The function changeBackColor calls drawn with the argument this (this=0x1fffc4d3 first image), inside the function draw, immediately after call, the value of this is different (this=0xffc4d3 second image).

changeBackColor function

drawn function

Adittional information: I have no leak in heap memory; I can not reproduce the problem in unit tests; The problem only appears when running the entire system.



Solution 1:[1]

Thanks for all help. About suggests: For this situation, it is not possible to post the entire code; The optimization was disabled.

The solution:

This is the code for the drawn function:

static void drawn(TextField * this)
{
  size_t cps = strlen(this->text);
  size_t cls = this->length - cps;
  uint8_t nx = this->px + (this->font->width + 1) * cps;
  char str[] = SPACES;
  str[cls] = '\0';

  if (this->trimSpaces)
  { \\...
  }
  \\...
}

this->length is a property that holds the length of the textField. I do not know the reason, but I got a miss behavior on strlen(this->text). Its returning a number greater than length, than cls became to high, and the instruction: "str[cls] = '\0';" is overwriting the memory in the same position of the first byte of the variable this.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cesar Berci