GC and dispose



Hi,

When I create an instance of one of my classes inside a method, does it
make a difference if I call its Dispose method?

public class Foo : IDisposable
{
public Foo
{
}

public Foo anotherFoo = null;

public void Dispose()
{
}
}

Given the class above is there a difference between the two examples
below? When will the GC give back the memory? Does ExampleA confuse GC
when it comes to tracking and releasing foo2?

public void ExampleA()
{
Foo foo1 = new Foo();
Foo foo2 = new Foo();
Foo foo3 = new Foo();
foo1.anotherFoo = foo2;
foo1.anotherFoo = foo3;
}

public void ExampleB()
{
Foo foo1 = new Foo();
Foo foo2 = new Foo();
Foo foo3 = new Foo();
foo1.anotherFoo = foo2;
foo2.Dispose();
foo1.anotherFoo = foo3;
foo1.Dispose();
foo3.Dispose();
}

Thanx,

--
Petros
.



Relevant Pages

  • Re: Has Java become too complicated?
    ... Static fields of an inner class would, logically, act exactly ... public class Foo ...
    (comp.lang.java.programmer)
  • Re: Logging introduction
    ... public class Foo ... It's important to have a sensible format, and consistent guidelines for how to construct the log messages. ... The example above shows a simplified approach of providing enough context to the message to aid information obtained via the descriptor-mandated format without too much clutter. ...
    (comp.lang.java.programmer)
  • Re: Running Java applications with C libraries trough JNI on HP-UX
    ... Try running your program under strace or truss or similar, ... $ strace -o strace.txt java Foo ... Exception in thread "main" java.lang.UnsatisfiedLinkError: ...
    (comp.lang.java.programmer)
  • Re: XmlSerializer and Security
    ... .NET framework 1.1 - no service pack ... Additional information: Security error. ... Also, just to be sure, is the foo class in the ...
    (microsoft.public.dotnet.security)
  • Re: 2005 Conversion Warning
    ... Public Class Foo ... Public Shared Sub DoBar() ...
    (microsoft.public.dotnet.languages.vb)

Loading