473,473 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

release mode static initialization error


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.
Jul 21 '05 #1
4 2189
roger <xr**@rogerware.com> wrote:
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:


It sounds like you're probably running into beforefieldinit problems.

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html
and
http://www.pobox.com/~skeet/csharp/singleton.html

Did you really get the exception with the code you gave, rather than
*slightly* different code? I can't see how the constructor would throw
a NullReferenceException anyway...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
See http://www.pobox.com/~skeet/csharp/beforefieldinit.html


Indeed. The use of a static constructor

static DataCache() { instance = new DataCache(); }

as opposed to declaring

private static instance = new DataCache();

has apparently solved the problem.

The thing that is disturbing to me is the difference in
behavior between DEBUG and RELEASE configurations.

Seems like if it's wrong, it should be wrong in both modes.

Or am I just being naive?


Jul 21 '05 #3
roger <xr**@rogerware.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
See http://www.pobox.com/~skeet/csharp/beforefieldinit.html
Indeed. The use of a static constructor

static DataCache() { instance = new DataCache(); }

as opposed to declaring

private static instance = new DataCache();

has apparently solved the problem.


Good.
The thing that is disturbing to me is the difference in
behavior between DEBUG and RELEASE configurations.
That doesn't disturb me *too* much - basically the JIT doesn't make as
many optimisations when it's running in the debugger. (I suspect if you
ran the debug code outside the debugger, you'd see the same as with the
release code.)
Seems like if it's wrong, it should be wrong in both modes.

Or am I just being naive?


While I can see how that would be desirable, it would have unpleasant
side effects - people would be confused when stepping through code to
find the type initialiser run at the start of a method rather than when
the type is first used, for instance. Maybe it would be better to
educate people than to hide that from them though...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:

That doesn't disturb me *too* much - basically the JIT doesn't make as
many optimisations when it's running in the debugger. (I suspect if you
ran the debug code outside the debugger, you'd see the same as with the
release code.)
No. I run the debug release outside the debugger all the time.
Never saw it.
Seems like if it's wrong, it should be wrong in both modes.

Or am I just being naive?


While I can see how that would be desirable, it would have unpleasant
side effects - people would be confused when stepping through code to
find the type initialiser run at the start of a method rather than when
the type is first used, for instance. Maybe it would be better to
educate people than to hide that from them though...


I doubt that would be all that confusing. Certainly something
people would learn and understand.
In that regard, it would help the education process along.

And would be much less confusing than having an application that
behaves differently owing only to the change in compilation mode.
This time I was lucky - the application crashed immediately.
Much worse are the ones that don't fail outright, but just produce
subtly incorrect results that may or may not be noticed...

Jul 21 '05 #5

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

Similar topics

4
by: Hagay Lupesko | last post by:
Hi, I've encountered a strange phenomena which appears to me as a bug: I have an engine that uses a System.Threading.Timer to invoke a delegate every X minutes. The code looks something...
7
by: Srinivasa Rao | last post by:
I have read in one article that when we compile the application in release mode, all the debug classes and properties will be automatically removed from the code. I tried to implement this thing by...
4
by: roger | last post by:
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...
0
by: Russ Barrett | last post by:
Hello, I'm new to ASP.Net and I'm trying to interface an ASP.Net (C#) application to functionality resident in a C++ static lib. From posts I've seen around I understand this requires creation of a...
2
by: Epetruk | last post by:
Hello, I have a problem where an application I am working on throws an OutOfMemoryException when I run it in Release mode, whereas it doesn't do this when I am running in Debug. The...
7
by: =?Utf-8?B?R3JpZ3M=?= | last post by:
Hello, After getting some posts on forums.microsoft.com but no solution I was asked to post over here. Hopefully someone here can help with my problem. I have a Windows Forms application...
5
by: sujeet | last post by:
Dear friends, I'm facing a strange problem... My Application is in Debug mode, and the third party static library i'm using is in release mode. When i compile and try to link my...
1
by: IanWright | last post by:
I thought I read that there was a separate place to ask questions about DLLs but couldn't find it, so I thought I'd ask it here as its C/C++... I think I might have my concept of how a DLL (regarding...
3
by: =?Utf-8?B?bG10dGFn?= | last post by:
We have developed a number of different applications (ASP.NET web site, Windows services, DLLs, Windows forms, etc.) in C# 2.0. We have developed and unit tested all these applications/components...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.