Resource leak (handles count) in simple .NET application

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
 
P

Peter Duniho

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
 
P

Peter Ritchie [C# MVP]

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

Teremock

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
 
T

Teremock

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
 
T

Teremock

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
 
P

Phil Wilson

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
 
T

Teremock

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

Teremock

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

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