Link to home
Start Free TrialLog in
Avatar of nateroberts
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!
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

You can set the initial capacity for the StringBuilder.  Are you doing that?  This is a buffer-overrun check, BTW.

Bob
Avatar of nateroberts
nateroberts

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
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
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
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
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(logLine)
            Application.DoEvents() 'FAILURE OCCURS HERE (line 30)
      End If
'...continues
End Sub

Stack Trace:
System.IndexOutOfRangeException: 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.Win32Native.lstrcpy(StringBuilder dst, IntPtr src)
   at System.Runtime.InteropServices.Marshal.PtrToStringAuto(IntPtr ptr)
   at Microsoft.Win32.SystemEvents.GetUserPreferenceCategory(Int32 msg,
IntPtr wParam, IntPtr lParam)
   at Microsoft.Win32.SystemEvents.OnUserPreferenceChanged(Int32 msg, IntPtr
wParam, IntPtr lParam)
   at Microsoft.Win32.SystemEvents.WindowProc(IntPtr hWnd, Int32 msg, IntPtr
wParam, IntPtr lParam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
   at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
   at System.Windows.Forms.Application.DoEvents()
   at PPMTool.sdAHTool.sdLogger.Log(String textToLog) in C:\Documents and
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMTool\sdLogger.vb:line 30
   at PPMTool.sdAHTool.TriangleReports.GenerateReport() in C:\Documents and
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMTool\TriangleReports.vb:line 117
   at PPMTool.sdAHTool.Form1.generateReports() in C:\Documents and
Settings\Nathan Roberts\My Documents\Visual Studio
Projects\ActiveHealth\PPMTool\Form1.vb:line 511
   at PPMTool.sdAHTool.Form1.SelectFolderButton_Click(Object sender,
EventArgs e) in C:\Documents and Settings\Nathan Roberts\My Documents\Visual
Studio Projects\ActiveHealth\PPMTool\Form1.vb:line 614
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

Any ideas?

Thanks,
Nate
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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