Click to See Complete Forum and Search --> : Explicitly Deleting


yagiska
February 3rd, 2002, 04:21 PM
Has anyone found a way within .Net to delete an object from memory explicity?

I'm writing an application that needs to delete object A whether or not other objects still have a reference to it, and it needs to delete them when I tell it to.

If not, then here's a couple of questions.

What happens in C++ if you do the above? Object B's reference to Object A now points to null? For the lifetime of Object B?

What would happen if I wrote a simple function in C++ to delete objects and had my C# code call it as an extern? Would the GC flip out?

This is driving me crazy as I move on to some pretty advanced stuff with the app, and still can't find a way to delete an object. Any help is greatly appreciated.

Thanks
Yagi

yagiska
February 9th, 2002, 02:48 AM
I've come up with a new idea for handling this problem. Hopefully someone will have some idea on how to implement it :)

I'd like to have a wrapper class for an object, like so.

class Wrapper
{
protected object target;
public Wrapper(object obj)
{
this.target = obj;
}
}

I'd like to somehow use reflection to then pass calls to Wrapper along to the target. I'm not sure this is possible, but something like this.

class Wrapper
{
protected object target;
public Wrapp(object obj)
{
this.target = obj;
}
}

public class SomeObject
{
public void SomeMethod()
{
}
}

public class App
{
public static void Main()
{
SomeObject obj = new SomeObject();
Wrapper wrap = new Wrapper(obj);
wrap.SomeMethod();
}
}

Then, I could just pass the wrapper around my program, and when I wanted to delete the object, I could really just set the target to null.

This of course means that I can't just have wrapper reflect the object type. I need it to work almost like a proxy I guess, but I honestly know nothing about proxies, so I may be wrong.

Anyone know of any built-in .Net classes that work like this, or am I going to have to really dig in and build my own?

Also, is it possible to override constructors?

SomeObject obj = new SomeObject();

So that obj actually points to a Wrapper, but thinks it's a SomeObject?

Thanks alot,
Yagi

sampson
February 11th, 2002, 06:02 PM
Hi,

I have had a play with this but I cannot seem to find a solution. I don't think .NET was ment to work this way. I have a couple of points which may help with some thoughts.

1) You can overload constructors but only with operators they always return a type which is the same as the class type. So a SimpleObject constructor always returns a SimpleObject.

2) It is not possible to have default properties in .NET. Therefore you will not be able to use wrapperObject.someMethod() unless that is a defined method for that object. Although you could write this just to call wrappedObject.SomeMethod()

3) It may be worth while looking at the IDisposable interface which means you can write code like

using (MyClass m = new MyClass)
{

}

and the object will automatically call the Dispose method at the end of the block. This can be used to free any resources and you should also call GC.SuppressFinalize(this) so that the Finalizer is not called.

4) If you use a wrapper object as described below you could implement a method to free the wrapped object by setting the reference to null and then calling GC.Collect() method. This is used to force the garbage collector to collect the maximum amount of memory it can. This may be a long process however.


If this was any use or you find a solution let me know I find this problem interesting.

Andy

Andrew Sampson
Director
Softsteel Solutions Limited.
Registered in England No.4092969
Registered Office: 81 Freedom Road, Sheffield S6 2XA.
Telephone 0114 233 8136
http://www.softsteel.co.uk/

dky1e
September 13th, 2002, 11:23 AM
Have you tried enforcing singleton class through static variables?
That way you would only have only one pointer to worry about and if you set it to null it should be marked for garbage collection.


//singleton pointer
private static myClass INSTANCE_ = new myClass();

public static myClass getInstance()
{
if( INSTANCE_ == null )
INSTANCE_ = new myClass();

return INSTANCE_;
}


Now whenever you need and instance of the myClass class then you call getInstance() method.

myClass c = myClass.getInstance();