B
Bern McCarty
If I have a class that implements the singleton pattern like so:
public __gc class AppManager {
private:
AppManager(); // private constructor
public:
virtual ~AppManager();
static AppManager* i = new AppManager(); // this initializer won't ever
run unless the class is referenced from an initializer for global data
..
..
..
....then I get a System::NullReferenceException the first time that I try to
access the singleton instance through AppManager::i. However, if I add a
reference to the AppManager class from an initializer for global data, then
the class variable initializer is run and everything works just fine. For
example if I simply add:
static gcroot<AppManager*> *ensureInitializers4ClassVariablesAreRun = new
gcroot<AppManager*>(AppManager::i);
....then I'm good to go. I can step through _CRT_INIT and see all the right
initialization happen.
Shouldn't my class variables for my __gc class be guaranteed to be
initialized by the time that I make my first reference to the class no
matter how/where that reference is made from?
Bern McCarty
Bentley Systems, Inc.
public __gc class AppManager {
private:
AppManager(); // private constructor
public:
virtual ~AppManager();
static AppManager* i = new AppManager(); // this initializer won't ever
run unless the class is referenced from an initializer for global data
..
..
..
....then I get a System::NullReferenceException the first time that I try to
access the singleton instance through AppManager::i. However, if I add a
reference to the AppManager class from an initializer for global data, then
the class variable initializer is run and everything works just fine. For
example if I simply add:
static gcroot<AppManager*> *ensureInitializers4ClassVariablesAreRun = new
gcroot<AppManager*>(AppManager::i);
....then I'm good to go. I can step through _CRT_INIT and see all the right
initialization happen.
Shouldn't my class variables for my __gc class be guaranteed to be
initialized by the time that I make my first reference to the class no
matter how/where that reference is made from?
Bern McCarty
Bentley Systems, Inc.