Hi
I have a resource leak in the very simple project.
Project contains Form with one TextBox control in multiline mode.
After start this app I open Task Manager and see "Handles" count incremental
on every 1 second.
The source code is following:
public partial class Form1 : Form
{
System.Threading.Timer tm;
public Form1()
{
InitializeComponent();
// start timer 1 second interval ...
tm = new System.Threading.Timer(OnTimer, null, 0, 1000);
}
void OnTimer(Object state)
{
// add text in thread safe mode ...
AddTerminalText(" Resource Leak!!! ");
}
delegate void AddTextCallback(string text);
AddTextCallback d;
object[] arg = new object[1];
private void AddTerminalText(string text)
{
if (textBox1.InvokeRequired)
{
if (d == null)
{
d = new AddTextCallback(AddTerminalText);
}
arg[0] = text;
textBox1.Invoke(d, arg);
}
else
{
textBox1.AppendText(text); // add text into control
...
}
}
}
I don't understand what's wrong
Please help anybody !!!
Roman
Phil Wilson - 17 Mar 2008 22:02 GMT
Is this a release build?

Signature
Phil Wilson
[MVP Windows Installer]
> Hi
>
[quoted text clipped - 48 lines]
>
> Roman
Teremock - 18 Mar 2008 11:12 GMT
It is not release of course :-)
It is simple demo that shows a problem.
> Is this a release build?
> > Hi
[quoted text clipped - 49 lines]
> >
> > Roman
Phil Wilson - 18 Mar 2008 17:41 GMT
No, I mean that GC behaves differently between release builds and debug
builds.

Signature
Phil Wilson
[MVP Windows Installer]
> It is not release of course :-)
> It is simple demo that shows a problem.
[quoted text clipped - 54 lines]
>> >
>> > Roman
Teremock - 20 Mar 2008 08:41 GMT
I'm sorry for my misunderstanding :-)
I tested in both modes "release" and "build".
Also I run app directly (without VS)
And I see same result in all cases - handles is leaking.
:-)
> No, I mean that GC behaves differently between release builds and debug
> builds.
Peter Duniho - 17 Mar 2008 22:14 GMT
> I have a resource leak in the very simple project.
> Project contains Form with one TextBox control in multiline mode.
> After start this app I open Task Manager and see "Handles" count
> incremental
> on every 1 second.
Not that the code you posted is really the right way to do what it seems
to be doing, but I don't see anything that would cause a leak. The main
point here is that the Task Manager isn't a very good way to observe
resource consumption of a .NET application, because .NET is doing things
itself to manage resources and may look like it's failing to clean up when
in fact it's just delaying the clean up.
It's entirely possible that whatever you're looking at, it's just a false
positive with respect to "leaking".
Pete
Teremock - 18 Mar 2008 11:23 GMT
Thanks fr your reply.
I have done some more tests.
Handlers counter grow up to 2500 (!!!!) value. After that it resets to 200
and grow up to 2500 again nad so on.
I afraid what will happen when several copies of such code will work in my
future multithreaded "server" application ...
Thanks
Roman
> > I have a resource leak in the very simple project.
> > Project contains Form with one TextBox control in multiline mode.
[quoted text clipped - 13 lines]
>
> Pete
Peter Duniho - 18 Mar 2008 20:19 GMT
> Thanks fr your reply.
>
> I have done some more tests.
> Handlers counter grow up to 2500 (!!!!) value. After that it resets to
> 200
> and grow up to 2500 again nad so on.
That suggests to me that, indeed, all you're seeing is delayed collection
of the resource. That's not a leak.
> I afraid what will happen when several copies of such code will work in
> my
> future multithreaded "server" application ...
I recommend that you worry about the "problem" if and when it actually
becomes one. .NET isn't perfect, but it's served quite a wide variety of
high-performance applications very well so far, and the odds of you
running into some fundamental limitation in .NET with respect to your own
application are pretty slim.
Much more likely is that you yourself will design or code some sort of
flaw that causes problems. Worry about those before you worry about .NET.
Pete
Peter Ritchie [C# MVP] - 17 Mar 2008 22:39 GMT
The handles being "leaked" or OS event handles, which won't get freed until
the next GC.
If you want to avoid those handles, use the WinForms timer component
instead; it will not cause the handle count to increase like that.

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> Hi
>
[quoted text clipped - 47 lines]
>
> Roman
Teremock - 18 Mar 2008 11:37 GMT
Thanks for reply.
It seems like handles are allocated in "textBox1.Invoke(d, arg);" call.
It is big unpleasant surpise.
I can't use WinForm.Timer. I created this demo special for test and find a
problem with leak in my big main multithreaded application :-(
Roman
> The handles being "leaked" or OS event handles, which won't get freed until
> the next GC.
[quoted text clipped - 10 lines]
> >
> > The source code is following:
Peter Ritchie [C# MVP] - 18 Mar 2008 16:25 GMT
> I can't use WinForm.Timer.
why not?
Teremock - 20 Mar 2008 08:49 GMT
Because WinForm.Timer works in main thread of application.
But I need to simulate a situation when control is changed from OTHER thread.
Therefore I use Threading.Timer in this test application :-)
> > I can't use WinForm.Timer.
>
> why not?