R
roger
Here's a weird one...
The code below works just fine when I build in DEBUG mode.
Today, I tried to build my solution in RELEASE mode, and
immediately fell over this problem - apparently something
in the order or sequencing or something regarding static
initialization.
Consider this class, which implements a singleton pattern:
public class DataCache {
private DataCache(int flag) {
Console.WriteLine("DataCache ctor " + flag);
}
public static DataCache Instance {
get {
#if true
bool b = instance == null;
return b ? (instance = new DataCache(1)) : instance;
#else
return instance;
#endif
}
}
private static DataCache instance = new DataCache(0);
}
- With the #if true code disabled, I get the following error
Unhandled Exception: System.TypeInitializationException:
The type initializer for "DataCache" threw an exception. --->
System.NullReference
Exception: Object reference not set to an instance of an object.
at DataCache..ctor(Int32 flag)
at DataCache..cctor()
--- End of inner exception stack trace ---
at DataCache.get_Instance()
at Server.Main(String[] args)
- With the #if true code enabled, I never see the DataCache
ctor being called with flag==1. I still see it called with
flag value as 0.
This tells me that in the Instance method, b was false, meaning
that the instance member was not null.
Of course, I was not able to reproduce this problem in a simple
test case. Maybe there's something about the rest of the setup
that I have (difficult to explain/post) so I thought that I'd first
check and see if this sounds familiar to anybody.
Again, to stress the point that I see nothing like this when
compiling in DEBUG mode. The private static instance is valid
and returned as you'd expect.
Any ideas?
(using Visual Studio 2003 framework version 1.1.4322 - fwiw)
Thanks.
The code below works just fine when I build in DEBUG mode.
Today, I tried to build my solution in RELEASE mode, and
immediately fell over this problem - apparently something
in the order or sequencing or something regarding static
initialization.
Consider this class, which implements a singleton pattern:
public class DataCache {
private DataCache(int flag) {
Console.WriteLine("DataCache ctor " + flag);
}
public static DataCache Instance {
get {
#if true
bool b = instance == null;
return b ? (instance = new DataCache(1)) : instance;
#else
return instance;
#endif
}
}
private static DataCache instance = new DataCache(0);
}
- With the #if true code disabled, I get the following error
Unhandled Exception: System.TypeInitializationException:
The type initializer for "DataCache" threw an exception. --->
System.NullReference
Exception: Object reference not set to an instance of an object.
at DataCache..ctor(Int32 flag)
at DataCache..cctor()
--- End of inner exception stack trace ---
at DataCache.get_Instance()
at Server.Main(String[] args)
- With the #if true code enabled, I never see the DataCache
ctor being called with flag==1. I still see it called with
flag value as 0.
This tells me that in the Instance method, b was false, meaning
that the instance member was not null.
Of course, I was not able to reproduce this problem in a simple
test case. Maybe there's something about the rest of the setup
that I have (difficult to explain/post) so I thought that I'd first
check and see if this sounds familiar to anybody.
Again, to stress the point that I see nothing like this when
compiling in DEBUG mode. The private static instance is valid
and returned as you'd expect.
Any ideas?
(using Visual Studio 2003 framework version 1.1.4322 - fwiw)
Thanks.