First off, there are some similar questions to this, but none really address the exact issue:
So, here's my question: I'm working on a VST plugin, and I have a class that is defined and implemented within a DLL. Multiple instances of this same DLL are loaded, and what I'd like to do is maintain an "instance count" that monitors how many times the class is constructed (which only occurs once, when the DLL is loaded per the VST standard.)
One straightforward way of doing this would be to create a class static variable that I initialize to 0 and increment in the constructor / decrement in the destructor. I'm confident that I know when my class is constructed and destructed, but what I'm unsure of is whether or not this class static variable will be shared across instances of my DLL.
To clarify, I am loading the same DLL multiple times; within the DLL is a class (used only within the DLL code, and not exposed to the application.) There is some discussion on whether or not the behavior of data defined in DLLs varies between Windows and Unix, so I'd like to know if doing this kind of thing within a DLL is safe for cross platform use.
An example class, defined within the DLL, and not exposed in any way to the application loading the DLL (or otherwise.)
Header File
// Foo.h
# pragma once
class Foo {
static int s_InstanceCount;
public:
Foo();
~Foo();
};
And now the source file
// Foo.cpp
#include "Foo.h"
int Foo::s_InstanceCount = 0;
Foo::Foo() {
s_InstanceCount++;
}
Foo::~Foo() {
s_InstanceCount--;
if (s_InstanceCount == 0) {
// Do something
}
}
The constructor of Foo is only called when the DLL is loaded by an application (i.e ::LoadLibrary on Windows), and the destructor is only called when the DLL is freed (i.e ::FreeLibrary on Windows). Consider that guaranteed. Will s_InstanceCount be shared across calls to the Constructor?
EDIT: As Serge points out below, a process can't really load a DLL twice. So, when my DLL is loaded twice by one process, the Foo instances that get constructed exist within the same memory space used by the loading process. Maybe...