I’ve been having big problems in reproducing and finding the cause of a bug. The occurence seems entirely random, so I suspected an uninitialized variable somewhere. But then I found this piece of code:
CMyClass obj; // A
obj.DoStuff();
if ( somebool )
{
CMyClass obj; // B
obj.DoStuff();
}
obj.DoOtherStuff();
It seems as though DoOtherStuff() is either done on “B”, or that B.DoStuff() sometimes actually works on A – i.e. i DoStuff() is actually called on the first obj.
Could this happen? I don’t think I got a compiler warning (I’ve fixed the code now in hoping that it might help). It seems very likely that this piece of the actual code is where the bug I’m trying to find is, but there could of course be other reasons I haven’t discovered yet.
The code, as written, should work. The first call to
DoStuff()and the last call toDoOtherStuff()can only be sent toA.The call to
DoStuff()inside theif(somebool) { }block can only be sent toB.From the standard:
And:
That being said, perhaps this is not what was intended by the author of that code. If the variables have the same name, perhaps the intent is to have only one instance of that variable, and the
Binstance created inside the loop is a mistake. Have you gone through the logic to see if a second instance makes sense?