Global xSemaphoreHandle corrupted

fabiensf wrote on Thursday, August 08, 2013:

Hi all,

I use xSemaphoreHandle as global variable in my program.
Declaration is the next:
xSemaphoreHandle xComSemaphore in main.c
extern xSemaphoreHandle xComSemaphore in all other .c files.

I check its value when I create it: vSemaphoreCreateBinary( xComSemaphore);
But in my interrupts file, when I check its value in next function  xSemaphoreGiveFromISR(xComSemaphore, &xHigherPriorityTaskWoken ); I saw that the value was different from the one during creation.

For information, I don’t use any Compiler optimization.

I know that  xSemaphoreHandle is a pointer to a xQUEUE structure, but I don’t understand why this value had change? Is it forbidden to use global variable for xSemaphoreHandle?

Thanks for your help.

rtel wrote on Thursday, August 08, 2013:

Is it forbidden to use global variable for xSemaphoreHandle?

It is just a variable, like any other variable.  The compiler has no knowledge of the kernel, so there is no way the kernel could impose any restriction on where or how the variable is defined.  The rules for defining them are just the same as for defining any other C variable, and your extern declaration should not be a problem.

I assume, as your program links, you have declared the variable at file scope, and not on the stack of a C function (which would be wrong, because when the function returns the stack is removed so the variable no longer exists).

I would guess this is just a simple RAM corruption somewhere, maybe caused by an incorrect linker script, or a stack overflow, or something similar.

Do you have stack overflow checking turned on?

If you have a fancy CPU and debugger then you should be able to set a data watch point on the variable and have the debugger automatically break at the point where the variable is written to.  Failing that, simply add the variable to a watch window, then step through the code until you find the value of the variable inexplicably changing to try and determine what is writing over it.


fabiensf wrote on Thursday, August 08, 2013:

Hi Richard,

One more time, you help me a lot.
I added a data watch point I saw that a I tried to write inside a array smaller than my index and overwrite the semaphore…

Thanks for your feedback.