When to use "Close" and when "Dispose"

  • Thread starter Thread starter Ofer B.
  • Start date Start date
O

Ofer B.

Hi

I had some memory problem with my application.
One of the reason was: not closing a data reader but it didn't solve all the
problems.
When I make more than one order, I can see the momory statuse decrease.
I use a global class that keeps some of the object that I use. Object like
the "Order" form.

To start an order I use this function:

if(Global.CurrentOrder == null)
{
Global.CurrentOrder = new Order(this.cboCustomers.Text);
Global.CurrentOrder.Show();
}
else
{
Global.CurrentOrder.Show();
}

To end an order I use:

Global.CurrentOrder.Close();
Global.CurrentOrder = null;

What do I do wrong? When do I use "Form.Close" and when Dispose?

Thanks Ofer
 
Your code should be fine, CurrentOrder should be garbage collected; you can
use Dispose which should dispose/free it up immediately, instead of waiting
for garbage collection.

Are you seeing a memory leak? Can you stress test it to watch the usage
over time? My guess is the objects just haven't been disposed yet by the
collector.
 
Brian:

Doesn't Close call Dispose()?
Brian said:
Your code should be fine, CurrentOrder should be garbage collected; you can
use Dispose which should dispose/free it up immediately, instead of waiting
for garbage collection.

Are you seeing a memory leak? Can you stress test it to watch the usage
over time? My guess is the objects just haven't been disposed yet by the
collector.
 
Back
Top