static members not initialized before static properties are called?

  • Thread starter Thread starter Ryan Steckler
  • Start date Start date
R

Ryan Steckler

I found this behavior while trying to implement a singleton class. Below is
a somewhat more straight forward example, though admittedly less useful in
real life. The basic problem is that when a static property of a class is
called (StaticInit.MyProperty), the property code executes BEFORE the static
members are initialized. This would lead one to assume that the static
members would equate to null, but that isn't the case either. They are in
some limbo state of uninitialization. Below is a simple class showing the
problem:

Ryan Steckler
Senscom, Inc.


using System;

//This class shows a bug in the language/framework/runtime?
//Apparently, calling a static property does not initialize static members
first. Static members do not equate
//to null, but throw null pointer exceptions when accessed.
//This can be shown by calling this class via:
//int n = StaticInit.MyProperty; //Broken.
//and
//int n = StaticInit.GetMyProperty(); //Works (if called *before* the
preceeding line).

namespace staticinit
{
public class StaticInit
{
//This Arraylist should be initialized to null (<undefined value>)
by the runtime.
private static System.Collections.ArrayList m_al; //adding a " =
null" makes no difference.

public StaticInit()
{
}

public static int MyProperty
{
get
{
if (m_al == null)
{
//The code *never* reaches this point.
m_al = new System.Collections.ArrayList();
}

//Accessing m_al at this point throws a null pointer
exception.
return 1;
}
}

public static int GetMyProperty()
{
if (m_al == null)
{
//The code comes in here the first time GetMyProperty is
executed, as expected.
//UNLESS someone called StaticInit.MyProperty before this
call. In that case, we don't come in here
//and references to m_al *still* result in null pointer
exceptions.
m_al = new System.Collections.ArrayList();
}

return 1;

}
}
}
 
<"Ryan Steckler" <rsteckler at s_e_n_s_c_o_m dot com (remove all
underscores)> said:
I found this behavior while trying to implement a singleton class. Below is
a somewhat more straight forward example, though admittedly less useful in
real life. The basic problem is that when a static property of a class is
called (StaticInit.MyProperty), the property code executes BEFORE the static
members are initialized. This would lead one to assume that the static
members would equate to null, but that isn't the case either. They are in
some limbo state of uninitialization. Below is a simple class showing the
problem:

<snip>

I cannot reproduce your problem. Static members should be null before
any extra initialization takes place.

Could you give a *complete* example which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for what I mean.

Note that if a class is marked with beforefieldinit, which is will be
unless you've got a static constructor (as opposed to just static
variable initializers), a static property *can* start executing before
the static initializers are run - although in my experience they're
not. However, the static initializers *will* be run before the first
field access.

See http://www.pobox.com/~skeet/csharp/beforefieldinit.html for more
information on that.
 
Jon-
Your advice helped me greatly. After trying to write a small app that completely showed the problem, I determined that the code
works fine, as long as it isn't running in the debugger.
Here is a small application that exhibits the problem. Note that I'm running in the VS.NET 2003 IDE debugger. When the breakpoint
is turned off, you can see that only one line is output to the console saying that the property has been accessed. When you
breakpoint the designated line in Main(), you can see that the property *appears* to have been called multiple times.

<code>

using System;
namespace StaticInitProblem
{
//StaticInit shows the debugger executing code
//differently when a breakpoint is active.
//To show the problem, set a breakpoint where
//indicated in Main().
public class StaticInit
{
public StaticInit()
{
}

public static int MyProperty
{
get
{
Console.WriteLine("Property accessed.");
return 1;
}
}
}

class StaticInitProblem
{
[STAThread]
static void Main(string[] args)
{
//First, run this app with no
//breakpoints and view the output.
//To show the problem:
//1) Breakpoint the next line of code.
//2) Run the application until it hits the breakpoint.
//3) Debug->Continue execution.
//4) Look at the application output window.
int i = StaticInit.MyProperty;
Console.WriteLine("Press enter to bail.");
Console.Read();
}
}
}

</code>

The interesting side effect I saw was due to the fact that even though the debugger appears to execute the property code multiple
times, on the "real" entry into that code, the static members were not null, but not fully initialized.

Ryan Steckler
Senscom, Inc.
 
<"Ryan Steckler" <rsteckler at s_e_n_s_c_o_m dot com (remove all
underscores)> said:
Your advice helped me greatly. After trying to write a small app that
completely showed the problem, I determined that the code works fine,
as long as it isn't running in the debugger. Here is a small
application that exhibits the problem. Note that I'm running in the
VS.NET 2003 IDE debugger. When the breakpoint is turned off, you can
see that only one line is output to the console saying that the
property has been accessed. When you breakpoint the designated line
in Main(), you can see that the property *appears* to have been
called multiple times.

Hmm... I'm afraid I still can't reproduce it. I'm still only ever
seeing one line output to the console (in terms of property access).

Anyway, I'm glad to hear it's only happening in the debugger. That's a
start at least...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top