I just upgraded to a new version of a third party library (QtPropertyBrowser for Qt 5.0). The upgrade led to a new bug in my application which I managed to track to a static function in the library. The function contains a static variable that is initialized the first time the function is called. I copied down the memory location of that variable after initialization and found, as expected, that on multiple subsequent calls that the variable remained in the same memory location. Then on a subsequent function call I noticed that the memory location and data in the static variable had changed (causing the bug in my program).
The code looks something like this:
class ClassA
{
//....
};
class ClassB
{
public:
ClassA* ptrMember;
};
static ClassA *theFunction()
{
static ClassB statVar = {0};
if(!statVar.ptrMember)
statVar.ptrMember = new ClassA();
return statVar.ptrMember;
}
I found that during multiple calls to theFunction() the address stored in &statVar always remained the same, but then on a subsequent call the address in &statVar was different and statVar.ptrMember was empty.
I didn't think this was possible! Any ideas?
&statVar) changes? Or that the pointer toClassAinside it changes? The second is easily explained since you overwrite it each time the function is called; the first would be more mysterious.ClassAeach time you call the function. Is that intended?