I have a number of static members of classes interacting. One of them never gets initialized.
I am trying to implement an common interface for audio devices, whatever the API. I register all APIs with a manager class. Since API objects and the manager objects are singletons I use static objects.
virtual uint32 GetDeviceCount() const = 0;
virtual AudioDevice* GetDevice(uint32 index) const = 0;
typedef std::map<String, AudioDeviceAPI*> APIMap;
static AudioDeviceManager& Get();
uint32 GetDeviceCount() const;
AudioDevice* GetDevice(uint32 DeviceIndex);
friend void AudioDeviceAPI::RegisterWithManager();
void RegisterAPI(const String& rAPIName, AudioDeviceAPI* pAPI);
static AudioDeviceManager* gpManager;
This is the (simplified) interface I use. The actual API implementations are subclasses of AudioDeviceAPI.
gpManager = new AudioDeviceManager();
void AudioDeviceManager::RegisterAPI(const String& rAPIName, AudioDeviceAPI* pAPI)
APIMap::const_iterator end = mAPIs.end();
APIMap::const_iterator it = mAPIs.find(rAPIName);
if (it != end)
mAPIs[rAPIName] = pAPI;
AudioDeviceManager* AudioDeviceManager::gpManager = NULL;
/* definition of AudioDeviceAPI_DirectSound goes here */
mName = "DirectSound";
static AudioDeviceAPI_DirectSound DirectSoundAPI;
The Problem: The static instance of AudioDeviceAPI_ASIO is initialized. The DirectSound one isn't... I first had to make the singleton pattern around the manager (with the NULL init, and static getter) to ensure that it would always be constructed before the AudioDeviceAPI instances. But the DirectSound API is just never instantiated. No cout, no breakpoint...
/* definition of AudioDeviceAPI_ASIO goes here */
mName = "ASIO";
static AudioDeviceAPI_ASIO AsioAPI;
I have found a workaround using a bogus function forcing instantiation, but I'd like to know why it doesn't work.
Working with Visual Studio 2005, if that'll help.