Solved

StringBuilder overflowed by unmanaged code

Posted on 2006-06-29
9
2,197 Views
Last Modified: 2010-05-18
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!
0
Comment
Question by:nateroberts
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17010367
You can set the initial capacity for the StringBuilder.  Are you doing that?  This is a buffer-overrun check, BTW.

Bob
0
 

Author Comment

by:nateroberts
ID: 17010433
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17010499
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
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:nateroberts
ID: 17010641
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17010686
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
0
 

Author Comment

by:nateroberts
ID: 17011548
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
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 17011700
What happens without the Application.DoEvents?  What is it's intended purpose?  Is it intended to increase system responsiveness?

Bob
0
 

Author Comment

by:nateroberts
ID: 17011903
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
0
 

Author Comment

by:nateroberts
ID: 17020212
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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question