Fix the crash on startup if instances are created before the instance

tracking is initialized.
This commit is contained in:
JoelKatz
2012-12-20 08:46:57 -08:00
parent 8e201277a6
commit ca25c6c3fb
3 changed files with 24 additions and 6 deletions

View File

@@ -32,6 +32,7 @@ protected:
InstanceType* mNextInstance;
static InstanceType* sHeadInstance;
static bool sMultiThreaded;
public:
typedef std::pair<std::string, int> InstanceCount;
@@ -42,17 +43,32 @@ public:
sHeadInstance = this;
}
static void multiThread()
{
// We can support global objects and multi-threaded code, but not both
// at the same time. Switch to multi-threaded.
sMultiThreaded = true;
}
void addInstance()
{
mLock.lock();
++mInstances;
mLock.unlock();
if (sMultiThreaded)
{
mLock.lock();
++mInstances;
mLock.unlock();
}
else ++mInstances;
}
void decInstance()
{
mLock.lock();
--mInstances;
mLock.unlock();
if (sMultiThreaded)
{
mLock.lock();
--mInstances;
mLock.unlock();
}
else --mInstances;
}
int getCount()
{