q23r,
Yes your problem is different. I didn't dig too deep, but here is what I
know.
First thanks for the sample code.
From what I see you create the controls and clean the collection in a loop.
Windows Forms controls use Win32 native controls for the UI and the native
controls are message based. In other words in order everything to works fine
there must be a running message pump (loop). When controls are created and
destroyed there are messages sent to the controls. When a message is sent
from code in the same thread as the one created the control there is no need
of running message pump; the control's wndproc is called directly. When a
message is sent across the thread boundaries though, the message is
delievered via the message queue.
In your sample you block the message pump for the duration of the test, so
no cross thread messages can be delievered to the control.
You may think that there is no another thread involved here, but there is
one.
If you don't call Dispose on the controls, their native windows are
destroyed from the control's finalizer by the GC. GC calls the finalizers
from a worker thread. Here is what probably happens, as I said I didn't dig
deeply to say for sure what goes wrong, but there is what I believe happens:
A window can be destroyed only from the thread that created it (this can be
found in the win32 docs) and because the finalizer run in the GC's worker
thread the call needs to be dispatched to the UI thread. .NET does that
using the message queue. This is cross thread message operation, so either
posted or sent there needs to be message pump running, but you've blocked
it. Because of that destroying the window and maybe the whole GC attempt to
free unamaged resource fails.
Anyways the solution is to dispose the control as soon as you remove them,
so the GC doesn't have to do that. It also works if you call
Application.DoEvents after Collection.Clear, but I think the disposing
method is better, of course the best would be not to do this in a loop.
Just to wrap it up I want to say that I wouldn't consider this as a BUG or a
memory leak.
--
HTH
Stoitcho Goutsev (100)
[quoted text, click to view] "q23r" <q23r@mail.ru> wrote in message
news:1151032900.962986.127830@y41g2000cwy.googlegroups.com...
> Stoitcho Goutsev (100) wrote:
>> I believe this is the same problem that you encountered
>
> Unfortunately it is not the same problem.
> In the bug you mentioned problem was realted to the Dock property.
> I simplified my example even more. As you can see there is no panel any
> more.
> May be there are another properties which default values prevents to
> release the memory?
> //////////////////////////////////////////////////////////////////////
> int i, j;
> for (i = 0; i < 150; i++)
> {
> for (j = 0; j < 200; j++)
> {
> this.Controls.Add(new Button());
> }
> this.Text = String.Format("i={0}", i);
> this.Controls.Clear();
> }
> //////////////////////////////////////////////////////////////////////
> On my machine i have exception when i = 48.
>