Solved

StringBuilder overflowed by unmanaged code

Posted on 2006-06-29
9
2,226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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