When to use Close method and when to use Dispose

  • Thread starter Thread starter NET CF Questions
  • Start date Start date
N

NET CF Questions

What are the rules on this?

I guess I'm not quite sure when to use the Dispose method.

I've seen code using both often, or using one or the other, but I'd
like some kind of guidelines on this.


Thank you.
 
I'd say that, if there's a Close method and that's what you want to do, call
it. If there's a Dispose method and that's what you want to do, call it.
There might be situations where you call them one after the other. No big
deal.

The situation where this comes up most-often is when using classes that
directly represent native objects (bitmaps, maybe, or Web connections).
There's been traffic in the past about various instances of Dispose, and you
can read up. Use Live Search or GoogleGroups:

http://groups.google.com/group/microsoft.public.dotnet.framework.compactframework/topics

Paul T.
 
If an object supports both, quite often Dispose will implicitly call Close
but it doesn't hurt to call them both. General rule is if an object exposes
them, you should call them when you're done using them.
 
And to add to the discussion, the Using construct is useful for ensuring
that objects do get disposed if an exception is thrown so you don't get to
Close/Dispose them yourself gracefully.

Ginny Caughey
Device Application Development MVP
 
And to add to the discussion, the Using construct is useful for ensuring
that objects do get disposed if an exception is thrown so you don't get to
Close/Dispose them yourself gracefully.

Ginny Caughey
Device Application Development MVP

And what if I don't call dispose?
Will garbace collector "clean it up" after function is ended?
 
Yes it will, but for most resources you'll want to have more control over an
object's lifetime. For example, you'll need to close a serial port or a file
before you can reopen it.

Ginny
 
Its always good practice - especially on memory contrained devices to clean
up and call Dispose and not to rely on the GC. When the GC runs it is a
*very* expensive operation - try to avoid it as much as possible. On the
desktop this doesn't matter so much.

On pre WinCE 6.0 which is beleive it or not is WM 6.1 and less than (yes WM
6.1 runs on WinCE 5.0) there is a 32 meg memory limit for each app. If you
hit this limit your app will start to become unstable as the device will
start to free memory up by sending WM_HIBERNATE to your application and if
required WM_CLOSE failing that it will terminate it.
 
Sorry, I meant to respond to everyone in this thread.

Thank you all so much for your input and advice.
 
And what if I don't call dispose?
Will garbace collector "clean it up" after function is ended?

Not necessarily. The GC collects only those objects whose reference
count equals 0, that is, those objects that are no longer referenced by
any other object.

However, consider the case where object A references object B, and
object B references object A. When you're done using both, the GC will
query them to know whether it should delete those objects. However,
object A will have a reference to object B, so object B will not be
deleted. And object B has also a reference to object A, thus object A
never gets deleted. Therefore, neither object A nor B gets deleted and
you have a memory leak.

That's why the disposable-pattern was designed for. When you call
Dispose, apart from freeing non-managed resources, you're supposed to
set to null other references, in cases when objects double-refernce
themselves. In our example above, object B must set its reference to
object A, to null; and vice versa. So the GC can do its job.

Regards.
 
The GC is more sophisticated.
If both objects A and B are not reachable they will be eventually
released.
It does more than just reference counting.
 
Back
Top