.NET Compact Framework System.Windows.Forms.UserControl is leaking memory

A

Armin Sczuka

Hello,

I figured out that all objects based on System.Windows.Forms.UserControl are
never disposed by the Compact Framework GC. This problem occours only under
Windows CE not with Win32/ Win64.

Is this a known issue?

Is there any fix for it?

How about the .NET Compact Framework 3.5? Is it fixed there?

Thanks,
Armin
 
G

Guest

And you have a repro? I've not heard of it as an issue and since the CF is
several years old, I'd suspect we'd have heard of someone having the problem
before now.
 
A

Armin Sczuka

Hi Chris,

if you create for example a new Console Application, add reference to System.Windows.Forms and run:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestGC
{
class Good
{
private byte[] _SomeData = new byte[1024];

~Good()
{
}
}

class Bad : System.Windows.Forms.Control
{
private byte[] _SomeData = new byte[1024];

~Bad()
{
}
}

class Program
{
static void Main(string[] args)
{
Good good;
Bad bad;

while (true)
{
good = new Good();
bad = new Bad();

System.Threading.Thread.Sleep(1);
}
}
}
}

If you run it an set a breakpoint on each finalizer you will see that the finalizer for Bad will never hit and the memory will go down (on your device).

Best Regards,
Armin
 
P

Peter Foot [MVP]

This is a Console app, can you repro it in a windows forms application?

Peter

--
Peter Foot
Microsoft Device Application Development MVP
www.peterfoot.net | www.inthehand.com
In The Hand Ltd - .NET Solutions for Mobility

Hi Chris,

if you create for example a new Console Application, add reference to System.Windows.Forms and run:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestGC
{
class Good
{
private byte[] _SomeData = new byte[1024];

~Good()
{
}
}

class Bad : System.Windows.Forms.Control
{
private byte[] _SomeData = new byte[1024];

~Bad()
{
}
}

class Program
{
static void Main(string[] args)
{
Good good;
Bad bad;

while (true)
{
good = new Good();
bad = new Bad();

System.Threading.Thread.Sleep(1);
}
}
}
}

If you run it an set a breakpoint on each finalizer you will see that the finalizer for Bad will never hit and the memory will go down (on your device).

Best Regards,
Armin
 
A

Armin Sczuka

Yes Peter,

It's the same there. Actually the objects seams to be collected if you
manually call IDisposable.Dispose().

/Armin
 
D

dbgrick

Armin,
You answered your own question. Just like for forms, you controls, you must
call dispose for the control to be collected. If you do not, then the
control will never be marked for collection. As as test you can create a
form and then not call the dispose in the finally method, and you will see
the memory never be deallocated. I've been called in on several projects
that were leaking memory and that is usually the reason for the leakage.

Regards,
Rick D.
Contractor
 
T

td

Hi,
I have just a question :
If my usercontrol contains controls, should I have to dispose all objects
before disposing my usercontrol ?
Thanks
 

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