I created an object, and I assigned it to two variables found in separate classes.
If in one class I set the variable to null, it only breaks that reference to the object but the object itself still persists due to the other reference.
How do I set the object to null, so that both references point to null? Without finding every reference.
this is pseudocode. The idea is to lose the reference in both ClassA (ref1) and ClassB (ref2).
public class ClassA
{
public ClassC ref1 = new ClassC ();
static void Main(string[] args)
{
ref1 = null;
}
}
public class ClassB
{
public static ClassC ref2;
public static AssignC (ClassC c)
{
ref2 = c;
}
}
public class ClassC
{
public ClassC ()
{
ClassB.AssignC (this);
}
}
You can't.
You cannot reach into the internals of another class, (well, not conventionally.) This form of Encapsulation makes .Net code much easier to debug.
You could write a function on the class that sets all its members to null
, you could do this as an implementation of IDisposable
.
When there are no references to an object the Garbage Collector will reclaim the memory, you cannot directly control this.
You could rewrite your example like this.
public class A
{
private C c = new C();
static void Main(string[] args)
{
c.Dispose();
c = null;
}
}
public static class B
{
private static C c;
public static LastC
{
get
{
return c;
}
set
{
c = value;
}
}
}
public class C : IDisposable
{
public C()
{
B.C = this;
}
public void Dispose()
{
if (B.LastC == this)
{
// LastC has not been set by another instance.
B.LastC = null;
}
}
}
You'll note that I've made the static reference accessible via a setter, so that other classes can access it.
Setting null to reference-type variable change only VALUE of that particular variable - reference to object. Good article by Jon Skeet - http://www.yoda.arachsys.com/csharp/parameters.html
In your case its better to wrap object into Singleton pattern. So the setting of null to main variable will destroy the only reference to object and it can be garbage-collected.
It is simply not possible.
References can be either null
or point to an actual instance. When you assign, as in ref2 = c;
the value of one reference is copied to another reference. After that you have two references to the same instance. (The instance is not copied, for sure, only the reference; these are reference types.)
When you do ref1 = null;
that just overwrites the particular reference with one that points to "nothing". It doesn't destroy any instance, just makes a single reference "point" to something else. Other references are unaffected (method parameters with ref
or out
of currently executing methods could in fact be the same reference, the one which is turned to points elsewhere).
In the question title you say "set the instance to null", but that doesn't really make sense, because an instance is an object (not a reference).
So how to destroy an instance? You can't. The garbage collector might come by at some point (when you are about to allocate a new object) and see that some instances have zero references to them. It will then remove the instances from memory.
You can't "zero" other people's references. Just because you have one references to an instance, there is no way to say "search through the entire application and find other references that happen to point to the same instance right now, and change all those references. That would be a really terrible thing to allow, because people could never be sure when their variables changed out of nowhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With