• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Corrupted NSMutableString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Corrupted NSMutableString


  • Subject: Re: Corrupted NSMutableString
  • From: Matthew <email@hidden>
  • Date: Sat, 2 Aug 2003 07:30:18 -0700 (PDT)

--- Cameron Hayne <email@hidden> wrote:
> On 28/7/03 4:08 PM, "Matthew" <email@hidden> wrote:
> > I'm working on a simple program that reads in data from a
> > binary file, processes the information, and copies it to a
> > plain text file (for processing in MATLAB). I can read all of
> > the data in fine. I can even print it without a problem. I'm
> > reading the data and appending it to a NSMutableString so that
> > I can use writeToFile to save the new text file. For some
> > strange reason, the NSMutableString gets corrupted. Only the
> > last two lines are important at the moment. The printf works
> > fine, but the appendFormat randomly chokes. (I've checked the
> > values by hand using a hex editor.)
> >
> > short int * zeroCalibrations[*numOfChannels];
> > short int * gainCalibrations[*numOfChannels];
> > short int trial[*numOfChannels];
>
> I know this isn't what you are asking about - but what are those
> arrays
> doing getting declared with what appears to be a variable?
> This is not valid C code (or Obj-C code) - you must declare
> arrays with sizes that are known at compile time. I recall that
> gcc might allow this sort of thing - but it should not.
> If you want dynamically sized arrays, you should allocate them.
> Or use NSArray, etc.

Done! I've converted them to NSArrays.

> > ...
> >
> > NSMutableString * matlabFile = [[NSMutableString alloc]
> > initWithString:@""];
> >
> > ... various loops ...
> >
> > [dataFile getBytes:trial range:NSMakeRange(*headerLength +
> > cellLength + (q * ((*numOfChannels * 2) *
> > *numOfSampPointsPerOb)) + (r * (*numOfChannels * 2)), 2 *
> > *numOfChannels)];
>
> That is a complicated range expression. It would be better to put
> it into separate variables, which you then could print out to
> check that you are not asking for more data than will fit in the
> array 'trial'.
> And I think you are treading on thin ice by using [NSData
> getBytes] to read short integers. You seem perhaps to be relying
> on the byte order and that sizeof(short int) is 2 ? Better not to
> build such dependencies into your code.

Since the binary file's definition uses 2-byte short integers (for this
section), I'm kind of stuck. Since C defines short integers as being
two bytes, it "should" not be a problem. (The byte order is specified
in the file, but I don't know how to swap the bits if I had to.) I have
stopped using the getData though. I've just added another loop so that
I'm just reading in one number at a time rather than a whole row of
numbers.

short * tempShort = [dataFile bytes] + offSet);
NSNumber * trialValue = [[NSNumber alloc]initWithShort:*tempShort];

I've stepped through the code. Everything is coming in fine.

>
> >
> > ... more loops ...
> >
> > printf("%.3f", (trial[s] - (float)*zeroCalibrations[s]) * 400 /
> > *gainCalibrations[s]);
> >
> > [matlabFile appendFormat:@"%.3f ", (trial[s] -
> > (float)*zeroCalibrations[s]) * 400 / *gainCalibrations[s]];
> >
> >
> >
> > (Please excuse the use of C type arrays, but they worked quite
> > well for reading a string of short integers...)
> >
> > After appending the 2472143rd float (-10.342), to the end of
> > matlabFile the string gets erased and the 2472144th element
> > (-17.289) becomes the first item. If I just put a blank space
> > after the %.3f, then the first string that I try to write comes
> > out ok, but the the second string gets truncated at a different
> > spot.
> >
> > At this moment, I'm at a loss. I've stepped through the file
> > while checking all the variables. Everything goes fine until
> > the string gets erased. The string should end up being about
> > 30MB, but gets cut down to 10.9MB. The following strings get
> > processed without a problem. If I make a simple change to what
> > get appended, then the location of the problem moves.
>
> It seems likely that you have some problem relating to not
> allocating enough space for your arrays and hence overwriting
> something.
>
> And anyway, why accumulate this all into one string if all you
> are going to do is write it to a file? Easier to read some data,
> format it, and write it out to the file before going on to read
> the next piece of data.
>
> --
> Cameron Hayne (email@hidden)
> Hayne of Tintagel
>

I would LOVE to write directly to a file, but I've had a few problems:

- How do you get NSString writeToFile: to append rather than overwrite
the existing file?

- I tried using a NSMutableArray, but when you writeToFile is saves the
data as a XML file. (170MB at that...)

- I've tried to use NSFileHandle writeData:, but it requires NSData. I
couldn't get the NSData encoding to write plain ASCII...

Thanks for your assistance!
Matthew
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Corrupted NSMutableString
      • From: Dustin Voss <email@hidden>
    • Re: Corrupted NSMutableString
      • From: "Alastair J.Houghton" <email@hidden>
References: 
 >Re: Corrupted NSMutableString (From: Cameron Hayne <email@hidden>)

  • Prev by Date: Getting rid of "multiple declarations"
  • Next by Date: NSImage -- getting raw bitmap data
  • Previous by thread: Re: Corrupted NSMutableString
  • Next by thread: Re: Corrupted NSMutableString
  • Index(es):
    • Date
    • Thread