tags:

views:

105

answers:

5

So, I have this project that creates multiple instances of a class, and list them.

At some point, the instanced class is not needed anymore. How can I flush it ?

So far, it doesn't happen, even when the class has nothing to do anymore (if it had been a single static class, the program would have shut down), it's still in my list, and its public variables are still available ...

+3  A: 

Why the concern to flush it? Why not just let .Net Garbage Collection do it's thing?

Altenatively you could implement IDisposable and call

MyClass.Dispose();

UPDATE: Sounds like you want a custom collection, not a list, that will only return active classes. Create a new class called MyClassCollection that implements ICollection and make sure it only ever returns active classes

brendan
I need the list to be up-to-date with only the active classes, and disregard the inactive ones.
Ben
@Ben what do you mean by "active classes"?
Pwninstein
+4  A: 

Your question implies that you have these instances in a list of some kind. Remove the instances you no longer want from the list, making sure you don't have any other references to them. At some point, the garbage collector will reclaim them.

Brian Clapper
So, the class that becomes inactive needs to somehow notify the class that holds the list to remove it from it ... so the garbage collector can dispose of it ... err ... right ? :)
Ben
If that's how your application is organized, yes.
Brian Clapper
@Ben, maybe you haven't given us all the info? How do we know when these classes have become inactive? Should the object be raising an event to say *"hey, i've got no more work to do, i'm no longer active"*?
slugster
Yes, I think that's the way it needs to go, like Cade Roux described below, sorry if I didn't give much more info, there was just not much to give, I was a bit clueless on how to proceed :)
Ben
+3  A: 

You can have your instances raise an event (OnInactivated, say). The list class should add a handler for such events when the item is added to the list (subclass or encapsulate a list and override the Add method). When the event fires, the object will be removed from the list by the list. Assuming the object is not referenced in any other lists (which might also be listening), then the garbage collector is free to deal with the object.

In this way, the object does not need to keep track of all references to itself, and know how to inform the reference holder to release the reference.

Here's a runnable example: http://www.coderun.com/ide/?w=Hw83i0wAQkugUif5Oj2lmw

Cade Roux
+1 i posted a message similar to this on @Brian's answer.
slugster
Thanks, that makes sense, I'll try to implement that.
Ben
@Ben - I've added a link to a runnable example.
Cade Roux
A: 

Example to hopefully explain what should happen:

public class MyClass {
    public string MyString { get; private set; }
    public int MyInt { get; private set; }
    public double MyDouble { get; private set; }

    public MyClass(string myString, int myInt, double myDouble){
        MyString = myString;
        MyInt = myInt;
        MyDouble = myDouble;
    }
}

public class SomeOtherClass{
    public List<MyClass> m_Instances = new List<MyClass>();

    public void FillList(){
        //Instantiate 3 items, and add to the list
        m_Instances.Add(new MyClass("1", 2, 3d));
        m_Instances.Add(new MyClass("4", 5, 6d));
        m_Instances.Add(new MyClass("7", 8, 9d));
    }

    public void RemoveFirst(){
        //Remove the first one. As long as the removed item has no
        //other instances, the Garbage Collector will (in its own time)
        //destroy that unused object, and reclaim the memory.
        m_Instances.RemoveAt(0);
    }
}
Pwninstein
A: 

A more general solution to referencing objects without preventing their garbage collection is to use a WeakReference.

Jacob