A bug in String.ToCharArray?

  • Thread starter Thread starter Michael Barnes
  • Start date Start date
M

Michael Barnes

I have encountered what appears to be a bug in
String.ToCharArray, and so far I have not found any
mention or discussion about it. I am interested to know
if anyone else is encountering this problem. I am
using .NET framework 1.1, and Visual Studio 2003. I can
reproduce the problem with a simple C# program:

using System;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string buffer = new string ( 'M', 12581653 );
char [] chs = buffer.ToCharArray ();
Console.WriteLine ( "chs Length = {0}",chs.Length );
}
}
}

This will throw an "out of memory" exception on the
ToCharArray call. I actually encountered this problem
trying to parse a large XML fragment - XmlTextReader uses
ToCharArray internally, as does XmlDocument, and both
will crash when presented with an Xml string 12,581,653
characters long.

Interestingly, the act of calling ToCharArray as above
adds about 500MB to the VM Size of my app, as shown in
Task Manager.
 
I have encountered what appears to be a bug in
String.ToCharArray, and so far I have not found any
mention or discussion about it. I am interested to know
if anyone else is encountering this problem. I am
using .NET framework 1.1, and Visual Studio 2003. I can
reproduce the problem with a simple C# program:

Michael,

I can confirm the problem on my machine. interestingly, the problem DOES NOT
occur under .NET 1.0. it seems that something was broken in .NET 1.1.

I hope someone from MS will finaly read this and at least confirm the
problem.

Regards,
Wiktor
 
Thanks for verifying that for me, Wiktor. I have reported it to
Microsoft as an "error in their documentation" :-) Hopefully that will
bring it to somebody's attention!
 
This is a known bug in GC. We will fail to allocate objects of certain
size.
The formula used to judge if a segment will be enough for an object was
wrong.
You can work around the problems the allocating a larger object (I
understand that's lame.)
If this is blocking you, please contact PSS to get a QFE.

Thanks,
Gang Peng
[MS]
 
Thank you, Gang Peng. I am glad that you are already on top of the
problem. I have taken the approach you suggested to work around the
problem - in my case, if the XML string that I am trying to parse falls
into the critical length, I just append some additional spaces to the
end. I agree that it is a bit of a rough fix, but it will do for the
time being.

Can you say what release of .NET will incorporate the GC bug fix?
 
Hi, Michael,
I checked the status of this bug with my team member who fixed this.
It is fixed as a QFE in August.
I can find the link to download the QFE tomorrow if you need it.

Gang Peng
[MSFT]
 
Thank you, but I don't want to rely on my customers having the right QFE
installed, so I will just leave the code work-around in place.
 
Back
Top