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

Re: Resource leak (handles count) in simple .NET application by Phil

Phil
Mon Mar 17 16:02:19 CDT 2008

Is this a release build?
--
Phil Wilson
[MVP Windows Installer]

"Teremock" <Teremock@discussions.microsoft.com> wrote in message
news:70F5ACAB-895D-4ED4-9927-6C19DC660068@microsoft.com...
> 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
>



Re: Resource leak (handles count) in simple .NET application by Peter

Peter
Mon Mar 17 16:14:16 CDT 2008

On Mon, 17 Mar 2008 13:29:02 -0700, Teremock
<Teremock@discussions.microsoft.com> wrote:

> 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

RE: Resource leak (handles count) in simple .NET application by PRSoCo

PRSoCo
Mon Mar 17 16:39:01 CDT 2008

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.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"Teremock" wrote:

> 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
>

Re: Resource leak (handles count) in simple .NET application by Teremock

Teremock
Tue Mar 18 05:12:02 CDT 2008

It is not release of course :-)
It is simple demo that shows a problem.

"Phil Wilson" wrote:

> Is this a release build?
> --
> Phil Wilson
> [MVP Windows Installer]
>
> "Teremock" <Teremock@discussions.microsoft.com> wrote in message
> news:70F5ACAB-895D-4ED4-9927-6C19DC660068@microsoft.com...
> > 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
> >
>
>
>

Re: Resource leak (handles count) in simple .NET application by Teremock

Teremock
Tue Mar 18 05:23:00 CDT 2008

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

"Peter Duniho" wrote:

> On Mon, 17 Mar 2008 13:29:02 -0700, Teremock
> <Teremock@discussions.microsoft.com> wrote:
>
> > 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
>

RE: Resource leak (handles count) in simple .NET application by Teremock

Teremock
Tue Mar 18 05:37:12 CDT 2008

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

"Peter Ritchie [C# MVP]" wrote:

> 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.
>
> --
> Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
> http://www.peterRitchie.com/blog/
> Microsoft MVP, Visual Developer - Visual C#
>
>
> "Teremock" wrote:
>
> > 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:
> >


RE: Resource leak (handles count) in simple .NET application by PRSoCo

PRSoCo
Tue Mar 18 10:25:04 CDT 2008

"Teremock" wrote:
> I can't use WinForm.Timer.

why not?

Re: Resource leak (handles count) in simple .NET application by Phil

Phil
Tue Mar 18 11:41:43 CDT 2008

No, I mean that GC behaves differently between release builds and debug
builds.
--
Phil Wilson
[MVP Windows Installer]

"Teremock" <Teremock@discussions.microsoft.com> wrote in message
news:9346489B-1099-4D7A-AFBE-4EB33A8ABFBB@microsoft.com...
> It is not release of course :-)
> It is simple demo that shows a problem.
>
> "Phil Wilson" wrote:
>
>> Is this a release build?
>> --
>> Phil Wilson
>> [MVP Windows Installer]
>>
>> "Teremock" <Teremock@discussions.microsoft.com> wrote in message
>> news:70F5ACAB-895D-4ED4-9927-6C19DC660068@microsoft.com...
>> > 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
>> >
>>
>>
>>



Re: Resource leak (handles count) in simple .NET application by Peter

Peter
Tue Mar 18 14:19:15 CDT 2008

On Tue, 18 Mar 2008 03:23:00 -0700, Teremock
<Teremock@discussions.microsoft.com> wrote:

> 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

Re: Resource leak (handles count) in simple .NET application by Teremock

Teremock
Thu Mar 20 02:41:00 CDT 2008

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.
:-)

"Phil Wilson" wrote:

> No, I mean that GC behaves differently between release builds and debug
> builds.
> --
> Phil Wilson
> [MVP Windows Installer]
>


RE: Resource leak (handles count) in simple .NET application by Teremock

Teremock
Thu Mar 20 02:49:00 CDT 2008

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 :-)


"Peter Ritchie [C# MVP]" wrote:

> "Teremock" wrote:
> > I can't use WinForm.Timer.
>
> why not?