Resource leak (handles count) in simple .NET application

  • Thread starter Thread starter Teremock
  • Start date Start date
T

Teremock

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
 
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
 
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.
 
It is not release of course :-)
It is simple demo that shows a problem.

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

Teremock said:
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
 
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
 
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
 
No, I mean that GC behaves differently between release builds and debug
builds.
--
Phil Wilson
[MVP Windows Installer]

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

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

Teremock said:
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
 
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.
:-)
 
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 :-)
 
Back
Top