473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Possible DataCoruption by using StringBuilder?!?

The code below was coded to to take the xml that is sent to and from a Data
Object in our system and strip out any strange characters that may creap into
the xml that is received from a Mainframe system. The code works fine, except
that we noticed some data coruption happening in the system, and have
subsiquently confirmed that it was this peice of code.

The xml would just get corrupted. We had parameters called index that would
suddenly be named iindex. (Notice two i's) The coruption was totally random
though, and would occure in variuous places in the xml document.

I once had a similar situation using the a Byte array to ascii converter
also in the System.Text namespace that turned out to only convert the first
128 bytes and then wrapped for any byte values greater than this.

If anyone could shed some light on this I would seriously apreciate it.

Dim TempSreamReader As System.IO.StreamReader = New
System.IO.StreamReader(stmXML)
Dim TempString As New
System.Text.StringBuilder(TempSreamReader.ReadToEn d())
TempString = TempString.Replace("& ", "& ")

Dim FindValues() As Char =
System.Configuration.ConfigurationSettings.AppSett ings("FindValues").ToCharArray()
Dim ReplaceValues() As Char =
System.Configuration.ConfigurationSettings.AppSett ings("ReplValues").ToCharArray()
Dim c As Int32 = 0
For c = 0 To FindValues.Length - 1
TempString = TempString.Replace(FindValues(c),
ReplaceValues(c))
Next

stmXML.Position = 0
Dim TempStreamWriter As System.IO.StreamWriter = New
System.IO.StreamWriter(stmXML)
TempStreamWriter.Write(TempString.ToString())
stmXML.Position = 0

Jul 21 '05 #1
6 1257
W1ld0ne74 <W1*******@hotmail.com> wrote:
The code below was coded to to take the xml that is sent to and from a Data
Object in our system and strip out any strange characters that may creap into
the xml that is received from a Mainframe system. The code works fine, except
that we noticed some data coruption happening in the system, and have
subsiquently confirmed that it was this peice of code.

The xml would just get corrupted. We had parameters called index that would
suddenly be named iindex. (Notice two i's) The coruption was totally random
though, and would occure in variuous places in the xml document.
It seems unlikely to me that it's actually a bug in StringBuilder. It's
hard to say more without some concrete code and a sample file, however.

When you say the corruption is totally random, do you mean that if you
give it the same file twice, it corrupts it in different places, or
just that you can't see any pattern in the corruption when giving it
different files?
I once had a similar situation using the a Byte array to ascii converter
also in the System.Text namespace that turned out to only convert the first
128 bytes and then wrapped for any byte values greater than this.


That's understandable, as ASCII doesn't have any values over 127 - you
shouldn't be using it with bytes which are over 127.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
This peice of code was added to a very high volume web site, so very
difficult to say if it would corrupt the same file in the same way. However,
we did not pick up this problem during testing (Aprox 3 users).

I noticed that StringBuilder is not threadsafe for instances. Could this be
the cause of the problem?

"Jon Skeet [C# MVP]" wrote:
W1ld0ne74 <W1*******@hotmail.com> wrote:
The code below was coded to to take the xml that is sent to and from a Data
Object in our system and strip out any strange characters that may creap into
the xml that is received from a Mainframe system. The code works fine, except
that we noticed some data coruption happening in the system, and have
subsiquently confirmed that it was this peice of code.

The xml would just get corrupted. We had parameters called index that would
suddenly be named iindex. (Notice two i's) The coruption was totally random
though, and would occure in variuous places in the xml document.


It seems unlikely to me that it's actually a bug in StringBuilder. It's
hard to say more without some concrete code and a sample file, however.

When you say the corruption is totally random, do you mean that if you
give it the same file twice, it corrupts it in different places, or
just that you can't see any pattern in the corruption when giving it
different files?
I once had a similar situation using the a Byte array to ascii converter
also in the System.Text namespace that turned out to only convert the first
128 bytes and then wrapped for any byte values greater than this.


That's understandable, as ASCII doesn't have any values over 127 - you
shouldn't be using it with bytes which are over 127.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
W1ld0ne74 <W1*******@hotmail.com> wrote:
This peice of code was added to a very high volume web site, so very
difficult to say if it would corrupt the same file in the same way. However,
we did not pick up this problem during testing (Aprox 3 users).

I noticed that StringBuilder is not threadsafe for instances. Could this be
the cause of the problem?


Well it would be if you were sharing the same StringBuilder between
different threads - are you doing that?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
I do not have it in a shared method or anything. The code is in a .Net
assembly that is used by the entire site (ASP.Net and other assemblies)
directly. Does .Net spawn a new thread for every new user that connects to
the ASP.Net application or could multiple users share the same thread to this
code and thus cause data coruption?

"Jon Skeet [C# MVP]" wrote:
W1ld0ne74 <W1*******@hotmail.com> wrote:
This peice of code was added to a very high volume web site, so very
difficult to say if it would corrupt the same file in the same way. However,
we did not pick up this problem during testing (Aprox 3 users).

I noticed that StringBuilder is not threadsafe for instances. Could this be
the cause of the problem?


Well it would be if you were sharing the same StringBuilder between
different threads - are you doing that?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5
W1ld0ne74 <W1*******@hotmail.com> wrote:
I do not have it in a shared method or anything.
What about stmXml? Is that shared, or is one created per request?
The code is in a .Net
assembly that is used by the entire site (ASP.Net and other assemblies)
directly. Does .Net spawn a new thread for every new user that connects to
the ASP.Net application or could multiple users share the same thread to this
code and thus cause data coruption?


Well, multiple users could get the same thread, but not while one
request is processing. Each request gets a thread pool thread (I
believe - an ASP.NET expert could say whether it's the system thread
pool or something else), so two requests could use the same thread, one
request after another, but you won't have two requests active on the
same thread at the same time. (If you think about it, you couldn't.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
Or is this in a "module" ?

Showing the minimal account of code that exhibit the problem may help...

Patrice

--

"W1ld0ne74" <W1*******@hotmail.com> a écrit dans le message de
news:25**********************************@microsof t.com...
I do not have it in a shared method or anything. The code is in a .Net
assembly that is used by the entire site (ASP.Net and other assemblies)
directly. Does .Net spawn a new thread for every new user that connects to
the ASP.Net application or could multiple users share the same thread to this code and thus cause data coruption?

"Jon Skeet [C# MVP]" wrote:
W1ld0ne74 <W1*******@hotmail.com> wrote:
This peice of code was added to a very high volume web site, so very
difficult to say if it would corrupt the same file in the same way. However, we did not pick up this problem during testing (Aprox 3 users).

I noticed that StringBuilder is not threadsafe for instances. Could this be the cause of the problem?


Well it would be if you were sharing the same StringBuilder between
different threads - are you doing that?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
7
by: Vincent Nguyen | last post by:
Hi, Does anyone know how call Win32 native API GetTokenInformation() by using C#? Any sample code would be helpful. Thanks! Vincent
3
by: veera | last post by:
Is it possible to give control focus using Asp.net code??
6
by: Shashi | last post by:
I have developed ASP.Net application using .Net 1.1 Framework. When the user clicks image file through Java script I am using my search window as below. QueryString =...
6
by: W1ld0ne74 | last post by:
The code below was coded to to take the xml that is sent to and from a Data Object in our system and strip out any strange characters that may creap into the xml that is received from a Mainframe...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
2
by: m00nm0nkey | last post by:
Ok well i thought i'd try a different approach, so what I'm now trying is appending 50,000 lines from the collection to a stringbuilder, and then writing that entire stringbuilder to a file. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.