|
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:
<code>
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 ...
}
}
}
</code>
I don't understand what's wrong
Please help anybody !!!
Roman
|
|
|
|
|
Hi,
I don't see anything wrong but have two comments:
1.
you could try the same with an empty OnTimer() and look for a difference in TaskManager.
2.
if that is the only thing the timer is supposed to do, you'd better use a Forms.Timer
which ticks on the GUI thread, hence no Invoke stuff required at all...
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi
Yes of course.
The empty OnTimer handler removes resource leak.
The using Timer from Forms namespace removes leak too.
You are absolutely right.
But problem is that I need to add string into text control from other (not main window) thread
This simple project is created specially for demonstrate a standard method for
add a text string into TextBox control from other thread!
This demo is based on standard sample from MSDN and it has leak!!!
I know that the permanent resource leak is in following part of source code:
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 ...
}
}
How I can add text correctly into text box from other thread ?
Roman
|
|
|
|
|
Hi Roman,
your code seems to be correct.
you are not dealing with handles yourself, the only thing you do is create some
objects; they will live their life, and end up being garbage collected. Nothing to
worry about.
you could try running it (with a faster timer, or a for loop somewhere) upto
millions of text lines; you will notice though that it slows down because a textbox
needs to concatenate text to do AppendText (a listbox would not really slow down
since it does not concatenate its items).
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi Luc
I will try to test more ... Maybe GC wil wake up and will do its job ....
The problem that I see leak after remove AppenText() method at all:
private void AddTerminalText(string text)
{
if (textBox1.InvokeRequired)
{
// it seems like leak somewhere here ..........
if (d == null)
{
d = new AddTextCallback(AddTerminalText);
}
arg[0] = text;
textBox1.Invoke(d, arg);
}
else
{
//Don not change Text of control !!!!!! Line is commented out !!!!! Do nothing!!!!
//textBox1.AppendText(text); // add text into control ...
}
}
Anyway all this does not look good ...
Roman
|
|
|
|
|
Hi Roman,
as I told you before, AppendText appends new text to existing text, so it has to
allocate a new string and copy both parts in there, generating at least one object;
and a growing one, the more appends you do. So the GC is bound to kick in sooner
or later. If you find it hard to believe I suggest you try with a ListBox instead.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi Luc
I undestand that AppendText do something with memory or other objects ...
And GC can work later.
Please note that my lalest source code does not call TextBox.AppendText(..)
and does not change Text property of control at all. But Handles are leaking!
It seems like leak is not related with Text property or with AppendText() method of TextBox class.
Problem with cross threading call.
It's very strange.
Anyway thank you very much!
Roman
|
|
|
|
|
Interesting!
I can't get it to work at all with AppendText . If I use
textBox1.Text += text;
then the handles count up to 155 then reset down to 145 and repeat. Presumably they're being mopped up on garbage collections, so they don't really leak.
Weird.
Regards,
Rob Philpott.
|
|
|
|
|
Hi Rob
You are right. It's very interesting.
I see leak even when I don't change Text in the control.
Please see my more detail reply to Luc above.
Roman
|
|
|
|
|
I have done more tests.
It seems like Handles counter grow up from 150 up to 2500 value.
After 2500 it resets back to 200 handles and increases again to 2500 and so on.
It is stupid behavior. Why "textBox1.Invoke(d, arg);" call takes system resourse (Handle).
Roman
|
|
|
|
|
Hi Folks,
I would like to make an application to access a network camera(separate IP, AXIS communication device) using C#. I just need to play the video on my desktop and not saving or anything.
I am new towards (High Level or dialog based)windows software development, so please help me and would be nice with some sample code examples.
BR,
Sher
|
|
|
|
|
Hi,
two suggestions:
1.
when new to a combination of issues, get acqainted to each of them individually.
So start creating a very simple app, then add the things you will need one by one.
2.
Look around either here at CodeProject or ask Google.
These are some camera related articles[^].
Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I want to take snap shot of Current application which is running on other machine.
Uptill now I successfully done to take printpreview of the current application form on local machine.
I want to take snap shot of the current screen.
How Can I go further?
|
|
|
|
|
Hello!
You need to create the small client application that caputures the
current desktop using GetWindowDC and GetWindow API.You can save these
bytes or send it to your sever application using SendMessage or by using
the sockets.
ritz1234
|
|
|
|
|
But How Can I get the Snap Shot of the Current Application running on a local machine using windows application.
Because I dont have idea about that.
Regards,
sjs4u
|
|
|
|
|
He just told you. If you can't understand what he said, then you probably can't get what you're looking for.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Please do not post your question more than one time.
Click[^]
Kristian Sixhoej
"Failure is not an option" - Gene Kranz
|
|
|
|
|
I am using VS 2003 to debug an web application.
It works well sometimes, but sometimes it does not. The status bar shows "Setting up server for debugging". It just keeps setting it up forever. I have also put localhost in the trusted sites but no help.
what can be the reason??
|
|
|
|
|
Well, it could be that VS2003 is as flakey as hell when debugging web projects on IIS. This happens to me all the time and I've just got used to either firing IISRESET from the command prompt or killing the ASP.NET worker process from task manager.
Regards,
Rob Philpott.
|
|
|
|
|
Well, I am new to this. Does this mean that have no ti live with this and that I just cannot do anything about it?
|
|
|
|
|
If its the same problem I have then you may have to get used to it. I think its something to do with shadowing in IIS which is a feature where you can deploy stuff to IIS without a break in service. Maybe you can switch it off - I haven't looked. First off, determine whether doing IISRESET after a build stops this problem or whether its something else.
Regards,
Rob Philpott.
|
|
|
|
|
Hello everyone,
I am looking for two tutorials, one for C# XML and the other for C# thread. I have performend search, but all I found are comprehensive ones, which involves other materials making the samples too big.
Do you have any ones to recommend which are dealing with pure XML rendering and pure threading control s much as possible?
BTW: I am a beginner with C# and my book does not cover the two topics too much.
thanks in advance,
George
|
|
|
|
|
Neither is very specific to a language. XML is a standard there is DOM API and SAX API they are very similar in every language I have used them in. Threading is threading and again not language specific. If you learn the concepts it is very simple to translate the language specific tools for your needs.
www.w3schools.com might have some short tutorials and samples on XML etc. If you do much work with XML you also need to learn XPath which you will also find there. You might also try Wikipedia for anything you need a basic introduction into.
For threading there is a site that people frequently give as a good location for learning. I don't remember it but if you search in the C++ / MFC forum you should find where people have given the URL. When I was trying to get a handle on threading I read Advanced Windows by Richter. I believe he has a .NET or even C# book out now that likely has similar threading coverage in it from the .NET platform perspective.
good luck
led mike
|
|
|
|
|
Thanks led mike,
1. For XML, I do not mean the basic concept, I mean how to use C# XML SDK/API. Any ideas or recommendations?
2.
led mike wrote: For threading there is a site that people frequently give as a good location for learning. I don't remember it but if you search in the C++ / MFC forum you should find where people have given the URL. When I was trying to get a handle on threading I read Advanced Windows by Richter. I believe he has a .NET or even C# book out now that likely has similar threading coverage in it from the .NET platform perspective.
MFC forum? Confused? Do you mean C++ threading or C# threading? Could you give me some hint about what you mean please?
regards,
George
|
|
|
|
|
I don't know about XML, but there's an excellent (and free) e-book on threading in C# here[^]. It's the first hit you get on google actually, so you might have found it already. I suppose it does require you to have some basic skill when it comes to c#, but I wouldn't recommend starting with threading when you're new to c# anyway. Good luck.
Standards are great! Everybody should have one!
|
|
|
|