Ways to delete an object if references are not known. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Ways to delete an object if references are not known.

yagiskayagiska Posts: 5Member
I posted this question on another messageboard, which I'll link below as a reference to save me posting space.

http://www.csharphelp.com/board/read.html?num=1&id=8373&loc=0&thread=8373

To summarize, I need a way to effectively delete objects, and I am unaware of all references to the object, so I can't just remove the references and let the GC collect it.

Ideally, I'd like to delete the object from memory, so that anything with a reference to it would then have a reference to null instead.

If I can't find a way to do this, I'm going to have to add a boolean field to the base object that represents its existence in my program and have all other developers check that. This in my mind is a hack, and a dangerous one, because it could really screw with things if someone forgot to check.

Thanks for any help,

Yagi

Comments

  • Eric TetzEric Tetz Posts: 2,141Member
    : To summarize, I need a way to effectively delete objects, and I am unaware of all references to the object, so I can't just remove the references and let the GC collect it.
    :
    : Ideally, I'd like to delete the object from memory, so that anything with a reference to it would then have a reference to null instead.

    You could create a wrapper object to control access to a shared instance:
    [code=ffffff]
    [color=000080][b]class[/b][/color] FoobarWard {
    [color=000080][b]public[/b][/color] FoobarWard(Foobar instance)
    { theInstance = instance; }

    [color=000080][b]public[/b][/color] Foobar get()
    { [color=000000][b]return[/b][/color] theInstance; }

    [color=000080][b]public[/b][/color] [color=000080]void[/color] destroy()
    { theInstance = [color=bb0000]null[/color]; }

    [color=000080][b]private[/b][/color] Foobar theInstance = [color=bb0000]null[/color];
    }
    [/code]
    Your other objects in your system are given FoobarWard objects instead of naked Foobars. You can then get rid of the underlying Foobar at any time by calling 'delete' on it's ward.

    All 'foobar.method()' calls in these other objects must be replaced with something like:
    [code=ffffff]
    Foobar fb = foobar.get();
    [color=000000][b]if[/b][/color] (fb != [color=bb0000]null[/color]) {
    fb.method();
    }
    [/code]
    or
    [code=ffffff]
    [color=000000][b]try[/b][/color] {
    foobar.get().method();
    } [color=000000][b]catch[/b][/color] (NullPointerException npe) {
    [color=80a0b0][italic]// ...[/italic][/color]
    }
    [/code]
    Cheers,
    Eric


  • Eric TetzEric Tetz Posts: 2,141Member
    P.S. I just noticed I was in a C# board, and not a Java board. The idea should still be applicable to C#.

    Cheers,
    Eric
  • yagiskayagiska Posts: 5Member
    This is the first thing about C# that's really frustrated me. Boo Boo for no delete keyword.
  • Eric TetzEric Tetz Posts: 2,141Member
    : This is the first thing about C# that's really frustrated me. Boo Boo for no delete keyword.

    If you want 'delete', use C++. A huge amount of a C++ programmer's time is spent dealing with memory management. Probably 30% of his code is dedicated to memory management alone. The ugliest bugs; memory leaks, memory corruption, etc., all stem from this source. He spends years learning techniques to avoid leaks, like using smart pointers to prevent leaks in the event of an exception, etc. He must learn to use sophisticated tools to save his ass when all precautions fail.

    All of this nightmare vanishes when you code in C#. Consider yourself grateful you don't have 'delete'. ;)

    Cheers,
    Eric
  • thebeingsthebeings Posts: 2Member
    I hope i am not incorrect on this.. Isn't c# similar to java? In that you cannot really destroy an object it must be collected...

    I know there was a hack for it in java but ... it didnt always work too well if you know what i mean..

    Well anyhow good luck!



    : I posted this question on another messageboard, which I'll link below as a reference to save me posting space.
    :
    : http://www.csharphelp.com/board/read.html?num=1&id=8373&loc=0&thread=8373
    :
    : To summarize, I need a way to effectively delete objects, and I am unaware of all references to the object, so I can't just remove the references and let the GC collect it.
    :
    : Ideally, I'd like to delete the object from memory, so that anything with a reference to it would then have a reference to null instead.
    :
    : If I can't find a way to do this, I'm going to have to add a boolean field to the base object that represents its existence in my program and have all other developers check that. This in my mind is a hack, and a dangerous one, because it could really screw with things if someone forgot to check.
    :
    : Thanks for any help,
    :
    : Yagi
    :

  • pingpongpingpong Posts: 937Member
    You can add the IDisposable interface to your classes and call .Dispose() whenever you want to delete them.

    Your implementation of dispose/finalize should look like this:

    [code]
    void Dispose()
    {
    MyFree();
    GC.SupressFinalize(this); // Tell GC to not worry about this object anymore.
    }

    ~MyClass()
    {
    MyFree();
    }

    private void MyFree()
    {
    // free your stuff over here.
    }
    [/code]

Sign In or Register to comment.