Jump to content
  • Advertisement
Sign in to follow this  
Sambori

[.net] Static Member Initialization

This topic is 3209 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
And i looked everywhere in the program there's no evidence of setting this dictionary to null, nor it's cleared.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!