Setting a reference to null, crash later

Hi all,

I think I may not understand something fully about C#.

In our code we have a long lived object, that is a global singleton. I'll call it "FrameworkSingleton". It has a static field called Instance holds a "FrameworkApp" object. At startup "FrameworkSingleton" creates a FrameworkApp object and holds it in it's "Instance" field, which is static. Now you can use Framework to always get the FrameWorkApp object. FrameworkSingleton only creates one FrameworkApp object, so you can just access it from anywhere pretty much now.

Now, I have a class that holds a reference to "FrameworkApp" as a property (in my object it's called "Framework"), it looks like this:

FrameworkApp Framework {get;set;}

At constructor time, in my object, I assign my Framework property by setting it to the singleton's instance:

Framework = FrameworkSingleton.Instance

Now, I have a problem

In some of my functions when I was done and I knew my object is "done" , I don't want things hanging on to the global instance, so I was setting "Framework" property to NULL

public void Cleanup() { Framework=null; }

Another developer said this was causing crashes because it was setting the Singleton's instance of Framework to null.

I was like um WTF wouldn't setting this to NULL only affect my property, which is just a reference?

Is there something I don't understand properly here?

by whatevermansure via /r/csharp

Leave a Reply