This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

a global variable is altered by non related functions

Hi,

There is a strange behavior in my project. I declare a global variable SwitchToBtFlag in project.h

#ifdef _project_c_
	#define AUTOEXT
#else
	#define AUTOEXT extern
#endif,

AUTOEXT volatile bit SwitchToBtFlag;

#undef AUTOEXT
This variable is set/clear by fun_a() and checked and cleared by fun_b(). Both functions are in different file.
void fun_a( void )
{
	…
	if( condition )
		SwitchToBtFlag = 1;
	else
		SwitchToBtFlag = 0;
	…
}

void fun_b( void )
{
	…
	if( SwitchToBtFlag )
	{
		SwitchToBtFlag = 0;
		…
	}
}
Somehow (I can not figure out) SwitchToBtFlag will be altered by some other functions that is totally not accessing (or related) to the variable. What possible reasons can cause this problem. Thanks for answering my question.

chao.

Parents Reply Children
  • Hi,

    I had another global variable that was enum type. When it was located in xdata space (the default addressing mode, large mode) the content would be altered by something. I tried to find out what altered it and got nothing. I am sure that it was not altered by stack overflowing, ISR or main stream functions. But, when I placed (using data) it in data space it was okay. What are the possible reasons for that? Thanks for answering my question.

    chao.

  • You could see that effect from an array index out of bounds, or bad pointer, potentially anywhere in the code. Moving the variable doesn't really fix the problem, but does change what gets stepped on. Check the old address of the variable in xdata and see if it is being written to.

    If you have an ICE with trace capability, (or a logic analyzer, if your xdata is really external), go back to the old code and set it up to trap writes to that address. Then see what address in the code is writing where it shouldn't.