ID: Q119328
1.00 1.50 WINDOWS kbprg kbbuglist
The information in this article applies to:
When a static member function with a local static object is called from two different object modules, two different objects are created. This problem occurs when all of the following three conditions are met:
For a local static object of an inline static member function, the optimizer does not take into account that the static member function may be called from other files.
To work around the problem, use one of the following suggestions:
-or-
-or-
Microsoft has confirmed this to be a bug in the products listed at the beginning of this article. We are researching this problem and will post new information here in theMicrosoft Knowledge Base as it becomes available.
However this is not a problem in Microsoft Visual C++ 32-bit edition.
The following sample code demonstrates this error.
/* Compile options needed : Default release options used by the
Visual Workbench,or any /O optimizations except /Od.
*/
// test.h
#include <iostream.h>
void TestFunctionA();
void TestFunctionB();
class Object
{
public:
int data;
Object();
~Object();
};
class Static {
public:
static int staticFunc();
};
inline int Static::staticFunc() {
static Object a;
return a.data++;
}
// test1.cpp
#include "test.h"
void main()
{
TestFunctionA();
TestFunctionB();
TestFunctionA();
TestFunctionB();
}
void TestFunctionA()
{
cout << "a.data = " << Static::staticFunc() << endl << flush;
}
// test2.cpp
#include "test.h"
Object::Object()
{
data=1;
cout << "Constructor call" << endl << flush;
}
Object::~Object() {
cout << "Destructor call" << endl << flush;
}
void TestFunctionB()
{
cout << "a.data = " << Static::staticFunc() << endl << flush;
}
/* Correct output (no optimizations) */
Constructor call
objectA.data = 1
objectA.data = 2
objectA.data = 3
objectA.data = 4
Destructor call
/* Wrong output (with optimizations) */
Constructor call
objectA.data = 1
Constructor call
objectA.data = 1
objectA.data = 2
objectA.data = 2
Destructor call
Destructor call
Additional reference words: 1.00 1.50 buglist1.00 buglist1.50 KBCategory: kbprg kbbuglist KBSubCategory: CPPLngIss Keywords : kb16bitonly
Last Reviewed: July 23, 1997