CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2002
    Location
    Ohio, USA
    Posts
    8

    Explicitly Deleting

    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



  2. #2
    Join Date
    Feb 2002
    Location
    Ohio, USA
    Posts
    8

    Different Route

    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


  3. #3

    Re: Different Route

    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/

  4. #4
    Join Date
    May 2002
    Posts
    121
    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.

    Code:
    //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.
    Code:
    myClass c = myClass.getInstance();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured