Monday, August 04, 2008

Local variable value changed only in release mode not in debug mode

I faced the problem like the following:
Local variable values are changed after the particular moment;The expected value is not placed in a variable;
Even though we are not changing the value;I faced this problem many times;But thought of it as a multi threading error;
Nature of Error:
---------------
This error is occured only in release mode not in debug mode;
and I have used the more local variables in a function;
Fix:
-------
I fixed the problem after calling the certain function the local variable value is changed;
void Test()
{
//variables ...
//....
//...
int i = 0;

i = 2;
InnerFn();
printf( i); // i value is around 158004
//Unknown reason
}
Solution:
--------------
it is not a multi threading error; Mostly the multi threading error affects only the global variables and class private or public members not a local variables;For Every Thread call, the stack variable is allocated and freed;
Problem is there is some memory leak in InnerFn() ; So it affects the local variables of a Test() fn;
After modified the memory leak problem in InnerFn(), the local variable value is not changed;the local variable have the expected value;

No comments: