[.net] Static Member Initialization

Started by
20 comments, last by phresnel 14 years, 5 months ago
Can someone tell me please why the heck C# initializes my static member whenever the class is instantiated? I'm not calling the member initialization code in the constructor, it's only declared as a static, and have the initialization in a static method. Actually it's a dictionary that I initialize once by calling that static method. Then Whenever I create an object of that class, the dictionary is set to null/cleared... Is there a way to prevent this "default" initialization of static member? Help appreciated.
Advertisement
Actually at some point of the program the object is disposed somehow...what the heck...I cannot even track down the object status because of VS debugging limitations...
Calm.

Testcase?
Quote:
Can someone tell me please why the heck C# initializes my static member whenever the class is instantiated? I'm not calling the member initialization code in the constructor, it's only declared as a static, and have the initialization in a static method. Actually it's a dictionary that I initialize once by calling that static method. Then Whenever I create an object of that class, the dictionary is set to null/cleared...

Post your code. The way you have described this problem makes little sense and suggests that you might be misusing or overloading a term.

Quote:
Actually at some point of the program the object is disposed somehow...what the heck...I cannot even track down the object status because of VS debugging limitations...

The debugger is very powerful. Don't confuse your lack of understanding of it with limitations on its part. You can watch managed objects out of scope by putting them in the watch window when they are in scope, right clicking them and choosing "make object ID." This will put {n#} after the object's value in the value column of the watch window (where n is some number, probably 1). Then you can type "1#" (no quotes) into the watch window and see that object regardless of whether or not you have a reference to that object in scope where execution is broken. You can track changes to an object pretty easily this way, and with conditional breakpoints.
I dunno what a test case I can use here. Debug break point cannot help trap any background event causing this object's destruction. It's static!!!!
Cannot keep watches of that object since they get lost as soon as the debug point is outside the module...
Ok I set the watch, and it seems that the object is freed and set to null as soon as the class is instantiated and a non-static member is initialized.

Posting code? Hmmmm nope...it's huge.
Example Testcase: Preferably minimal source code that reproduces the error.

Quote:It's static!!!!

Calm.


Quote:Posting code? Hmmmm nope...it's huge.

See this. If not, no help.
Quote:
I dunno what a test case I can use here.

The code for the class containing the static member in question, and one of the lines of code that instantiates an instance of that class in a way that causes your bug.

Quote:
Debug break point cannot help trap any background event causing this object's destruction. It's static!!!!

I doubt the object is being destroyed, you're misusing that term. You seem to really be claiming the static member that is a reference to that object is being set to null somewhere. A breakpoint can help you track this down; the member being static has nothing to do with it.

Quote:
Cannot keep watches of that object since they get lost as soon as the debug point is outside the module...

Yes, you can, using the method of tracking object IDs I described above. That's what it's there for. It maps a user-friendly ID to the object's internal handle inside the CLR. It's reliable.

Quote:
Ok I set the watch, and it seems that the object is freed and set to null as soon as the class is instantiated and a non-static member is initialized.

If something is setting the reference to null, that doesn't mean the underlying object is being freed, that's not how the GC works.

Quote:
Posting code? Hmmmm nope...it's huge.

Then you get no help. We cannot read your mind. Post the code I asked for in the beginning of this reply. Or a minimal reproduction case as phresnel is asking for. Without that we can't help you any further.
I tried to create little code that samples the problem...but it worked. I think now the complexity of the program confuses the GC somehow...until it's my fault, it's the GC :D

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
interface IAAA
{
Dictionary<string, int> X { get; }
}

class AAA : IAAA
{
private static Dictionary<string, int> x;

private Dictionary<string, int> y = new Dictionary<string,int>();

public static void Init()
{
x = new Dictionary<string,int>();
x["A"] = 10;
x["B"] = 20;
x["C"] = 30;
}

public Dictionary<string, int> X
{
get { return x; }
}
}

class Program
{
static void Main( string[] args )
{
AAA.Init();

IAAA a = new AAA();

Console.WriteLine( a.X["A"] );
Console.WriteLine( a.X["B"] );
Console.WriteLine( a.X["C"] );
}
}
}


X object is reinitialized...so after the the call new AAA(), the output is no longer 10, 20, 30 :)

This code works but my program does not.
And i looked everywhere in the program there's no evidence of setting this dictionary to null, nor it's cleared.

This topic is closed to new replies.

Advertisement