Memory Leak due to objects not destroyed

G

Guest

Hi everybody!
we developped an .NET CF application, and we experience a memory leak at
runtime which continues until having a out of memory. I work on it since a
few days, i've been googling a lot to see similar problems, but nothing
helped me.

The problem is in fact simple : I have some objects that are never destroyed
nor collected, and i really don't understand why. There is quite a great
number of forms (for an embedded app), about 50 (with a maximum of 5
instanciated at a time), we developped. I logged calls to constructs and
destructors for the forms and some other important objects of the
application. it appears that most forms
are well collected at runtime. But I have one (for what i know) that is
nether destroyed till we exit app. And it seems to be the same for a number
of objects, some of them beeing quite large objects with datatables.
Worst for the form, It can be called in various part of the app, and there
are calls that make the form collected. And the calls are always the same,
something like this :
using (Form myForm = new Form)
{
myForm.ShowDialog();
// Some code using returns of myForm
}

For all that i saw, and i looked for, there are no reference keeped on the
form, neither on the other objects i've found they're not collected.

I'm sure Garbage collection is made cause i logged memory usage
(GC.GetTotalMemory) and there are fluctuations (and as we are other 1Mo of
used RAM, it must be called very often if i understand).

I've succeeded in slowing down memory leak by overriding Dispose(bool
disposing) method in all our forms so that they set all referenced objects
specifics to the form in it to null, but it was not enough and it results
only a more linear curve.

There possibly always are reference(s) to uncollected objects, but i don't
know where, and i don't know any meaning to get the information.

Does anyone have some info that could help (a known issue of CF 1.0 SP3,
tools to help me found references, anything else...) ?

If you need more info, i can try to give you some. I would have prefered
being able to isolate the problem, but it seems to me it's a lot of work.

Thanks to anyone who can help ;).

Guillaume
 
C

Chris Tacke, eMVP

Forms shown with ShowDialog are not disposed when closed. This is
documented behavior. The idea is that they will be shown again and keeping
a reference allows them to be shown quickly. I also recall a bug where
disposing a Form didn't release all of the controls on the Form properly,
though that may be fixed.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
A

Alex Feinman [MVP]

That form you mention, can you confirm that it's Dispose method is indeed
called? Do you perform any cleanup in it? Are there any references to it in
other parts of the applications (look for collections)?
 
A

Alex Feinman [MVP]

Chris Tacke said:
Forms shown with ShowDialog are not disposed when closed. This is
documented behavior. The idea is that they will be shown again and
keeping a reference allows them to be shown quickly.

But he seems to be using "using" - that should take care of dispose, right?
I also recall a bug where disposing a Form didn't release all of the
controls on the Form properly, though that may be fixed.

That has been fixed in SP1 or SP2
 
S

Stelrad Doulton

just a quick thought....... does the problematic form register an event
handler on a long lived object?
 
G

Guest

But he seems to be using "using" - that should take care of dispose,

In theory, but it's worth actual verification, especially if there's a
suspected leak.

-Chris
 
G

Guest

Hi all!
First of all, thanks for your interest in this issue.

Dispose is called (i added a trace to check it).
No other references to it except in the using that i know.
There is in fact one possibility of having a reference, cause i have a
static linked list containing all active forms. Forms are removed from that
linked list on Closed event. I checked and the event is well called on
Disposing. So the linked list reference is not keeped (and it works for
others forms).

For registered events, I have to check it, but i don't think there are
events registered by global objects...
 
G

Guest

Thanks for this thought !

I checked and there was no direct event handler on a long lived object.
I have done one thing to try : I unregistered all events registered by my
code when Dispose is called.
The result is not bad (even if not really good for my memory leak) : the
form is now well destroyed, but it does not have a real influence on the
memory leak (after 20 loops of a 4 forms, no significant difference ).

So events management seems to be one cause for objects not to be collected.
I will try to go further in this side.

Is there a way to remove all registered event handlers on an object (like a
DataTable to take a sample) ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top