NSOperation崩溃

时间:2018-06-19 04:04:53

标签: ios objective-c nsurlsession nsoperation

每次都没有发生这次事故 我认为mutableData的地址无效或多线程访问mutableData的地址。它发生在记忆低的状态 大屏幕设备占大多数,如iphoneX,iphone8 plus,iphone 6plus,iphone7 plus;
系统版本高于11.0。

崩溃日志如下: 11线程 SIGSEGV SEGV_ACCERR

0 libsystem_platform.dylib  _platform_memmove + 48
1 Foundation    -[_NSInlineData initWithBytes:length:] + 56
2 Foundation    -[_NSInlineData initWithBytes:length:] + 56
3 Foundation    -[_NSPlaceholderData initWithBytes:length:copy:deallocator:] + 100
4 ZYLite    -[ZYMediaDataSourceDownloadOperation URLSession:task:didCompleteWithError:] (ZYMediaDataSourceDownloadOperation.m:59)
5 ZYLite    -[ZYMediaDataSourceManager URLSession:task:didCompleteWithError:] (ZYMediaDataSourceManager.m:261)
6 CFNetwork ___51-[NSURLSession delegate_task:didCompleteWithError:]_block_invoke.207 + 76
7 Foundation    ___NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
8 Foundation    -[NSBlockOperation main] + 72
9 Foundation    -[__NSOperationInternal _start:] + 848
10 libdispatch.dylib    __dispatch_client_callout + 16
11 libdispatch.dylib    __dispatch_block_invoke_direct$VARIANT$mp + 224
12 libdispatch.dylib    __dispatch_client_callout + 16
13 libdispatch.dylib    __dispatch_block_invoke_direct$VARIANT$mp + 224
14 libdispatch.dylib    _dispatch_block_perform$VARIANT$mp + 104
15 Foundation   ___NSOQSchedule_f + 376
16 libdispatch.dylib    __dispatch_client_callout + 16
17 libdispatch.dylib    __dispatch_continuation_pop$VARIANT$mp + 424
18 libdispatch.dylib    __dispatch_async_redirect_invoke$VARIANT$mp + 604
19 libdispatch.dylib    __dispatch_root_queue_drain + 588
20 libdispatch.dylib    __dispatch_worker_thread3 + 120
21 libsystem_pthread.dylib  _pthread_wqthread + 1176

以下代码:

#import "ZYMediaDataSourceDownloadOperation.h"
#import "ZYMediaDataSourceCacheItem.h"

@interface ZYMediaDataSourceDownloadOperation ()

@property (nonatomic, assign) NSUInteger receiveDataLength;
@property (nonatomic, assign) NSUInteger lastOffset;
@property (nonatomic, strong) NSMutableData *mutableData;
@property (nonatomic, strong) NSMutableData *backData;

@end

@implementation ZYMediaDataSourceDownloadOperation

#pragma mark - super

- (id)init {
    self = [super init];
    if (self != nil) {
        self.mutableData = [NSMutableData data];
        self.backData = [NSMutableData data];
    }

    return self;
}

#pragma mark - NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session
          dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    if (self.responseBlock) {
        self.responseBlock(response, self.object);
    }
}

- (void)URLSession:(NSURLSession *)session
          dataTask:(NSURLSessionDataTask *)dataTask
    didReceiveData:(NSData *)data {
    if (self.dataBlock) {
        self.dataBlock(data, self.object);
    }

    [self.mutableData appendData:data];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {

    NSData *data = nil;
    if (self.mutableData) {
        data = [self.mutableData copy];

        self.mutableData = nil;
    }

    if (self.completionBlock) {
        self.completionBlock(data, self.offset, error, self.object);
    }
}

@end

0 个答案:

没有答案