0

每当我将应用程序发送到后台时,我都会收到自旋锁错误。这是我最小化应用程序时的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    if (savedResults || savedSchedule || watchingClasses || professors) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex:0];
        NSString *fullPath = [docDir stringByAppendingFormat:@"/%@", kFileName];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:4];
        if (!savedResults)  {
            [array addObject:[NSNull null]];
        }else {
            [array addObject:savedResults];
        }
        if (!savedSchedule) {
            [array addObject:[NSNull null]];
        }else {
            [array addObject:savedSchedule];
        }
        if (!watchingClasses) {
            [array addObject:[NSNull null]];
        }else {
            [array addObject:watchingClasses];
        }
        if (!serverAnnouncements) {
            [array addObject:[NSNull null]];
        }else {
            [array addObject:serverAnnouncements];

        }if (!professors) {
            [array addObject:[NSNull null]];
        }else {
            [array addObject:professors];
        }
        [NSKeyedArchiver archiveRootObject:[array copy] toFile:fullPath];
    }
        //close connection
    if (outputStream) {
        if ([outputStream hasSpaceAvailable]) {
            dispatch_queue_t task = dispatch_queue_create("Close Connection", nil);
            NSString *str = @"_CLOSE_CONNECTION*\n";
            NSData *dataToSend = [[NSData alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
            dispatch_async(task, ^{
                [outputStream write:[dataToSend bytes] maxLength:[dataToSend length]];
            });
        }
    }
    [inputStream close];
    [outputStream close];
    inputStream = nil;
    outputStream = nil;
    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    initializedSocket = NO;

    [FlurryAds setAdDelegate:nil];
}

我在 ouputStream 写入方法上得到一个 EXC_BAD_ACCESS。我也附上截图。图片

4

1 回答 1

1

看起来像一个悬空指针。

该流在 ivar 中被引用,因此您调度的块仅保留self,而不是self->outputStream。当您在分派块后立即清除 ivar 时,对它的唯一强引用将消失,并且流在仍在使用时被释放,从而导致崩溃。

为避免此问题,请确保您的块通过使用本地范围的变量而不是 ivar 来维护对流的强引用:

NSOutputStream *os = self->outputStream;
dispatch_async
(
    ...,
    ^ {
        [os write:...];
    }
);

请注意,在这里为这一块创建一个调度队列是没有意义的;你应该只使用全局队列。

于 2013-01-18T19:13:00.923 回答