1 Reply
      Latest reply on Dec 16, 2016 4:35 AM by eskimo
      zy_chongqing Level 1 Level 1 (0 points)

        after updating the xcode to 8.2. I rebuild one of my old project, found the APP will crash when [NSData subdataWithRange], if call it many times (more than 50), it will crash. But the code is normal before, i did nothing change for it.


        the code as below:

        while (alreadySndLen < fileSize)


                sndDataLen = (unsigned int)MIN(SEND_SLICE_SIZE, (fileSize - alreadySndLen));


                cmdHead.msgOffset = (unsigned int)alreadySndLen;

                cmdHead.msgLength = sizeof(FILE_HEAD) + (unsigned int)sndDataLen;


                sndRange.location = alreadySndLen;

                sndRange.length   = sndDataLen;

                NSMutableData *cmdData = [[NSMutableData alloc] initWithBytes:&cmdHead length:sizeof(CMD_HEAD)];

                [cmdData appendBytes:&fileHead length:sizeof(FILE_HEAD)];

                NSLog(@"new file len:%lu", (unsigned long)fileData.length);

                NSData *subData = [fileData subdataWithRange:sndRange];

                if (nil == subData)


                    NSLog(@"sub data %@ is nil", NSStringFromRange(sndRange));




                    [cmdData appendData:subData];




                alreadySndLen += sndDataLen;


                float progress = (float)alreadySndLen/(float)fileSize;

                if (![[MMCMDMgr sharedCMDMgr] uploadFileSliceData:cmdData ofNode:memoID ofSlice:sliceID ofUser:usrID ofType:fileType ofProgress:progress])


                    return FALSE;



        • Re: crash in NSData subdataWithRange
          eskimo Apple Staff Apple Staff (13,925 points)

          There’s not really a lot to go on here.  A simple test indicates that -subdataWithRange: seems to be working in general.

          NSData * d = [@"Hello Cruel World!" dataUsingEncoding:NSUTF8StringEncoding];
          for (size_t i = 0; i < 100; i++) {
              NSData * sd = [d subdataWithRange:NSMakeRange(i % (d.length), 1)];
              NSLog(@"%@", sd);

          How does it actually crash?  Can you post a crash report?

          How is your data generated?

          If you extract that code into a simple test project, does it still crash?

          Have run with zombies?  What about address sanitiser?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"