“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.
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.