nateroberts
asked on
StringBuilder overflowed by unmanaged code
I'm using .NET Framework 1.0.3705 to build an automation tool that processes a lot of files, and keeps logs on what it's doing. At some point in a larger run (I don't have access to the user's full data set), a user received the following error message:
"Warning: A StringBuilder buffer has been overflowed by unmanaged code. The process may become unstable. Insufficient capacity allocated to the StringBuilder before marshaling it."
The question is: what might cause this? I'm not actually using StringBuilder directly (though I can see that maybe I should); I am concatenating some potentially large strings for logging and reporting.
Unfortunately, I don't know where in my code this is happening, only that it isn't happening on my smaller data set. In different places, I call TextBox.appendLine, textbox.text (set), StreamWriter.WriteLine, and StreamWriter.Write. Those are the only calls that I can think of that might use StringBuilder. (Does inline concatenation of strings use StringBuilder? I do that, too, of course: a = b & c.)
Extrapolating from my smaller data set, I'd estimate that the largest these strings could get is around 600,000 characters long. By default, StringBuilder has a maximum capacity of Int32.MaxValue (2,147,483,647) characters, according to this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemTextStringBuilderClassTopic.asp
So that's curious. I'm also curious about the error's claim that this is happening in unmanaged code. I thought that .NET was all managed code.
The user is presently running the job again (it takes a few hours) to regenerate output that he deleted. I might know more after he finishes that; if so, I will post here again.
Thanks in advance!
"Warning: A StringBuilder buffer has been overflowed by unmanaged code. The process may become unstable. Insufficient capacity allocated to the StringBuilder before marshaling it."
The question is: what might cause this? I'm not actually using StringBuilder directly (though I can see that maybe I should); I am concatenating some potentially large strings for logging and reporting.
Unfortunately, I don't know where in my code this is happening, only that it isn't happening on my smaller data set. In different places, I call TextBox.appendLine, textbox.text (set), StreamWriter.WriteLine, and StreamWriter.Write. Those are the only calls that I can think of that might use StringBuilder. (Does inline concatenation of strings use StringBuilder? I do that, too, of course: a = b & c.)
Extrapolating from my smaller data set, I'd estimate that the largest these strings could get is around 600,000 characters long. By default, StringBuilder has a maximum capacity of Int32.MaxValue (2,147,483,647) characters, according to this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemTextStringBuilderClassTopic.asp
So that's curious. I'm also curious about the error's claim that this is happening in unmanaged code. I thought that .NET was all managed code.
The user is presently running the job again (it takes a few hours) to regenerate output that he deleted. I might know more after he finishes that; if so, I will post here again.
Thanks in advance!
ASKER
Bob,
I'm not using StringBuilder directly at all -- that's what makes this mysterious to me. I'm calling other things (TextBox and StreamWriter, at least) that I imagine may use StringBuilder.
Thanks,
Nate
I'm not using StringBuilder directly at all -- that's what makes this mysterious to me. I'm calling other things (TextBox and StreamWriter, at least) that I imagine may use StringBuilder.
Thanks,
Nate
Nate,
Could you give me a little more information to go on? An exception stack trace? Something to examine why it is happening.
Would it be possible for you to upgrade from 1.0?
Bob
Could you give me a little more information to go on? An exception stack trace? Something to examine why it is happening.
Would it be possible for you to upgrade from 1.0?
Bob
ASKER
Bob,
I may be able to do that after the user finishes the present run. I have not been able to replicate the issue here.
I plan to upgrade to .NET 2003 soon (I am actually awaiting shipment). Do you think that might resolve this?
Thanks,
Nate
I may be able to do that after the user finishes the present run. I have not been able to replicate the issue here.
I plan to upgrade to .NET 2003 soon (I am actually awaiting shipment). Do you think that might resolve this?
Thanks,
Nate
1.0 was the first one out the door and had a lot of annoying bugs, but I can't confirm this one. 2003 might solve the problem, or even introduce a whole new set of problems--you just never know until you try. 2002/1.0 was something that was never found very viable, so it didn't last very long in our shop.
Bob
Bob
ASKER
The user ran the job again, and now I have some more details, including a stack trace. The point in my code where the exception is being thrown is noted below:
Public Sub Log(ByVal textToLog As String)
Dim timestamp As DateTime = DateTime.Now
Dim logLine As String = Now & " - " & textToLog & vbCrLf
If Not logTextBox Is Nothing Then
If Len(logTextBox.Text) + Len(logLine) > logTextBox.MaxLength Then
logTextBox.Text = logTextBox.Text.Substring( logTextBox .MaxLength / 2)
End If
logTextBox.AppendText(logL ine)
Application.DoEvents() 'FAILURE OCCURS HERE (line 30)
End If
'...continues
End Sub
Stack Trace:
System.IndexOutOfRangeExce ption: Warning: A StringBuilder buffer has been
overflowed by unmanaged code. The process may become unstable.
Insufficient capacity allocated to the StringBuilder before marshaling it.
at Microsoft.Win32.Win32Nativ e.lstrcpy( StringBuil der dst, IntPtr src)
at System.Runtime.InteropServ ices.Marsh al.PtrToSt ringAuto(I ntPtr ptr)
at Microsoft.Win32.SystemEven ts.GetUser Preference Category(I nt32 msg,
IntPtr wParam, IntPtr lParam)
at Microsoft.Win32.SystemEven ts.OnUserP referenceC hanged(Int 32 msg, IntPtr
wParam, IntPtr lParam)
at Microsoft.Win32.SystemEven ts.WindowP roc(IntPtr hWnd, Int32 msg, IntPtr
wParam, IntPtr lParam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at
System.Windows.Forms.Compo nentManage r.System.W indows.For ms.UnsafeN ativeMetho
ds+IMsoComponentManager.FP ushMessage Loop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.Threa dContext.R unMessageL oopInner(I nt32 reason,
ApplicationContext context)
at System.Windows.Forms.Threa dContext.R unMessageL oop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Appli cation.DoE vents()
at PPMTool.sdAHTool.sdLogger. Log(String textToLog) in C:\Documents and
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMT ool\sdLogg er.vb:line 30
at PPMTool.sdAHTool.TriangleR eports.Gen erateRepor t() in C:\Documents and
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMT ool\Triang leReports. vb:line 117
at PPMTool.sdAHTool.Form1.gen erateRepor ts() in C:\Documents and
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMT ool\Form1. vb:line 511
at PPMTool.sdAHTool.Form1.Sel ectFolderB utton_Clic k(Object sender,
EventArgs e) in C:\Documents and Settings\Nathan Roberts\My Documents\Visual
Studio Projects\ActiveHealth\PPMT ool\Form1. vb:line 614
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr olNativeWi ndow.OnMes sage(Messa ge& m)
at System.Windows.Forms.Contr olNativeWi ndow.WndPr oc(Message & m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
Any ideas?
Thanks,
Nate
Public Sub Log(ByVal textToLog As String)
Dim timestamp As DateTime = DateTime.Now
Dim logLine As String = Now & " - " & textToLog & vbCrLf
If Not logTextBox Is Nothing Then
If Len(logTextBox.Text) + Len(logLine) > logTextBox.MaxLength Then
logTextBox.Text = logTextBox.Text.Substring(
End If
logTextBox.AppendText(logL
Application.DoEvents() 'FAILURE OCCURS HERE (line 30)
End If
'...continues
End Sub
Stack Trace:
System.IndexOutOfRangeExce
overflowed by unmanaged code. The process may become unstable.
Insufficient capacity allocated to the StringBuilder before marshaling it.
at Microsoft.Win32.Win32Nativ
at System.Runtime.InteropServ
at Microsoft.Win32.SystemEven
IntPtr wParam, IntPtr lParam)
at Microsoft.Win32.SystemEven
wParam, IntPtr lParam)
at Microsoft.Win32.SystemEven
wParam, IntPtr lParam)
at System.Windows.Forms.Unsaf
at
System.Windows.Forms.Compo
ds+IMsoComponentManager.FP
Int32 pvLoopData)
at System.Windows.Forms.Threa
ApplicationContext context)
at System.Windows.Forms.Threa
ApplicationContext context)
at System.Windows.Forms.Appli
at PPMTool.sdAHTool.sdLogger.
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMT
at PPMTool.sdAHTool.TriangleR
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMT
at PPMTool.sdAHTool.Form1.gen
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMT
at PPMTool.sdAHTool.Form1.Sel
EventArgs e) in C:\Documents and Settings\Nathan Roberts\My Documents\Visual
Studio Projects\ActiveHealth\PPMT
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
button, Int32 clicks)
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
IntPtr wparam, IntPtr lparam)
Any ideas?
Thanks,
Nate
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The intended purpose is, indeed, to increase application responsiveness, so if the user has moved the window or whatever it'll move at this point, but it's also to get the text box to redraw.
I'll try doing logTextBox.Refresh() instead, and have the user try that. I'll post back as soon as I hear anything, probably in a couple hours.
Thanks,
Nate
I'll try doing logTextBox.Refresh() instead, and have the user try that. I'll post back as soon as I hear anything, probably in a couple hours.
Thanks,
Nate
ASKER
Bob,
I did as you suggested, and removed the DoEvents call, replacing it with a Refresh() call, and it now runs without exceptions being thrown. I also do plan to try version 1.1, in hopes that it will be less likely to exhibit random bugs such as this one. It's always frustrating to me when I don't understand *why* something has gone wrong, even if I find a way around it. I wish I at least understood the bug's behavior. But here's hoping I won't have cause to worry about this one again!
Thanks for your help!
Nate
I did as you suggested, and removed the DoEvents call, replacing it with a Refresh() call, and it now runs without exceptions being thrown. I also do plan to try version 1.1, in hopes that it will be less likely to exhibit random bugs such as this one. It's always frustrating to me when I don't understand *why* something has gone wrong, even if I find a way around it. I wish I at least understood the bug's behavior. But here's hoping I won't have cause to worry about this one again!
Thanks for your help!
Nate
Bob