I have a code base which run well on iOS and Android using Clang. Recently I migrate the code to Windows using Visual C++ 2019. There are several strange behaviors of cl compiler.
class Engine {
public:
static Engine* sharedInstance() {
Engine* aaa = Factory(); // Test code
static Engine* instance = Factory();
return instance;
}
static Engine* Factory();
};
where Factory is a static function implemented in each platform specific file return platform unique Engine subclass instance.
class EngineWindows: public Engine {
};
Engine* Engine::Factory() {
return new EngineWindows();
}
But when I debug. instance is always nullptr and cannot enter Factory() function. I create another non-static instance using 'Engine* aaa = Factory();'. I can enter Factory() function and aaa has valid pointer.
So strange.
Assembly code shows that for instance, there is no Factory call and the code jump away at "jle".
By the way, I turned on /Gw /GA /GL for my DLL. After I removed these flags. Everything goes fine, the instance successfully initialized. And the assembly codes look like this.