473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

__gc class variable initializers not run. VS 2003 bug?

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.
Nov 17 '05 #1
1 1126
Bern McCarty wrote:
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 .


This isn't valid native C++ so I don't know if it is valid Managed C++. Is
it explained in the Managed C++ Extensions that this is valid ? How about
trying:

AppManager * AppManager::i = new AppManager();

public __gc class AppManager {
private:
AppManager(); // private constructor
public:
virtual ~AppManager();
static AppManager* i; }

In other words, initialize out of class, which is what unmanaged C++ has to
do. Does that work correctly ?
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert A Riedel | last post by:
In a module DATA.CPP, when attempting to initialize a __gc array as follows: // // Begin sample ... // // // Yes, I include all of the correct assemblies, including MSCORLIB.DLL and all of...
2
by: Robert A Riedel | last post by:
This is a repost of a question that received no answer. In a module DATA.CPP, when attempting to initialize a __gc array as follows: // // Begin sample ... // // // Yes, I include all of...
2
by: Gueverson | last post by:
Hello, I am new in programming smart cards and I got an error in compiling that I cannnot manage to get rid of. There is a function in the PCSC that is called "ScardEstablishContext" I am calling...
3
by: m | last post by:
Hi, I'm reading Microsoft Visual C++ .NET Step by Step, Version 2003 and I found a sentence it says : "you can't have pointers to managed types as members of a __gc class.". Why ? Thanks,...
0
by: John Smith | last post by:
I made a wrapper in C++.NET for a dll I wrote in normal C++. Among it's functions this wrapper contains an Init() function which has the following prototype: static MY_STATUS...
4
by: Tom | last post by:
Does anyone know if the 2005 version of C++.NET will support the traditonal C-style way of initializing arrays at the declaration? . -- Tom
1
by: Bae,Hyun-jik | last post by:
Is member variable alignment in __gc class same to that of __nogc class? For example, does this code below make problems?(Please ignore other errors such as wrong grammar ,etc.) Please reply....
1
by: Achim Domma (Procoders) | last post by:
Hi, I still try to implement a .Net wrapper to handel AVI Files. I'm unsing managed C++ with VStudio 2003. The structure of my classes looks like this: class AudioStream { ... } class...
0
by: John Gardner | last post by:
Hello I've got some managed C++ and I would like to do something like this: Int16 data1 __gc = {1, 2, 3, 4, 5}; Int16 data2 __gc = {2,3,4,5,6}; Int16 bigArr __gc; bigArr->Add(data1);...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.