Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello All, I have the following

Status
Not open for further replies.

s2001

Programmer
Dec 7, 2001
132
0
0
US
Hello All,

I have the following line of code that creates text file. The problem is after about 5000+ recursion, i get StackOverflowException. Could you guys suggest me an alternative way or problem with the code? TIA

try
{
using (TextWriter tw = new StreamWriter(filename))
{
// write a line of text to the file
//StackOverflowException Error occurs here.
tw.WriteLine(_Html);
// close the stream
tw.Close();
}
}
catch (Exception)
{
throw;
}

Thanks,
MB
 
As a general question to go along with this... when is it necessary to dispose() ?
 
It is necessary to dispose of objects that work with unmanaged memory and/or are not part of a typical dispose cycle.

For example: if you open an image you are working with unmanaged memory through a managed wrapper. When you are done with the image you should call Dispose() on it.

If you add a picturebox to a form and set the image at design time, the form calls dispose on the picturebox for you during it's disposal. The picture box in turn calls dispose on its image when it disposes.

 
Hi Christiaan, but the code has the using{} block, which should automatically dispose the object (mark it free for gc to collect).. am i missing something.. thanks a lot to you guys for your responses..

Thanks,
MB
 
Then I think you need a gc.collect call. I have noticed that on very fast iterations the GC isn't following like it should and you get out of memoryexceptions. But since this is not the case for you (you get a stackoverflowexception) I would look for it elsewhere. Try running a profilre on it to see which object doesn't get disposed.

Is there any other code within the loop (I'm guessing there is since I can't see the loop code).

Christiaan Baes
Belgium

My Blog
 
Hi Christiaan,
Thanks a lot for your response. I will run mem profiler and check it out. I do not have much code in the loop. I am setting _Html variable with data from a function and passing it on to the writer.



Thanks,
MB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top