3

I'm getting a rare and intermittent crash which looks like appendBytes being called with -1 as it's length. now, I've hard coded the "length" argument every time I've used this method so I can't see how this could happen and worse still I can't see how I could test for and avoid this crash.

here's the top of the stack and the exception (note the ~4.2b length):

*** Terminating app due to uncaught exception 'NSMallocException', reason: '*** -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for length (4294967295)'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x91ec5a67 __raiseError + 231
    1   libobjc.A.dylib                     0x9950a149 objc_exception_throw + 155
    2   CoreFoundation                      0x91e2d289 +[NSException raise:format:arguments:] + 137
    3   CoreFoundation                      0x91e2d1f9 +[NSException raise:format:] + 57
    4   Foundation                          0x92d2489e _NSMutableDataGrowBytes + 1136
    5   Foundation                          0x92d24391 -[NSConcreteMutableData appendBytes:length:] + 354 

here's a simplified version of the code that's supposedly crashing:

        if (self.isConnectedToService) {
            NSMutableData *myData = [NSMutableData data];

                float newValue = PanValue;
                const char theTwo[] =  {(char)Chan_L, (char)PanParam};
                [myData appendBytes:&theTwo length:2];
                [myData appendBytes:&newValue length:4];
        }

So length is always 2 or 4.

I've tested different situations in which the buffers contain more or less than 2 and 4 and I've never managed to cause this crash intentionally.

I've got the same code running on both MacOS10.7.4 and iOS6.0(on iPad3) and see this issue occasionally on both platforms.

so how is appendBytes getting that bogus value?

  • This isn't the problem, but you are passing the address of theTwo to appendBytes, but it's already a pointer. So you're really just appending the address of theTwo, not the array contents. – monoxygen Feb 06 '13 at 05:06
  • really? appendBytes wants a pointer and I declare theTwo as "const char" so it's not already a pointer is it? – user2045184 Feb 06 '13 at 05:10
  • 1
    It is already a pointer because you also declared it as an array. Arrays are essentially pointers -- the variable points to the address of the first element. – monoxygen Feb 06 '13 at 05:16
  • so perhaps a more interesting question is: why does that code work for me? it's appending the expected bytes and working well. – user2045184 Feb 06 '13 at 05:28
  • oops, yeah, that's actually fine. The compiler is smart enough to know it's an array, so the '&' is a no-op on theTwo. – monoxygen Feb 06 '13 at 05:38
  • thanks for pointing that out (pun intended) – user2045184 Feb 06 '13 at 05:40
  • Really odd. I just copy and pasted your code and it works for me, no crash. Why don't you try dropping down to CoreFoundation and use the CFMutableData instead. It let's you pick which allocator to use. – Daniel Farrell Feb 07 '13 at 12:30
  • yeah, it's an intermittent crash - I run it all day and it crashes a couple of times out of thousands of calls - so it's really hard to diagnose and test. hence the question. good idea on the CoreFoundation. – user2045184 Feb 07 '13 at 21:03
  • I believe whatever you simplified out of the code above may be the culprit. Are there any other `-appendBytes:length:` calls anywhere else in your code? Or are you passing in the length parameter from a variable in your actual code? – Siobhán Feb 14 '13 at 00:22
  • Do you still have this problem? – Resh32 Mar 21 '13 at 15:35

0 Answers0