I am getting a spin lock error whenever I send my application to the background. Here is the code I have when the application is minimized:
- (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];
}
I am getting an EXC_BAD_ACCESS on the ouputStream write method. I am attaching screenshots as well. 
Looks like a dangling pointer.
The stream is being referenced in an ivar, and so your dispatched block only retains
self, notself->outputStream. When you clear the ivar just after the block is dispatched, the only strong reference to it is gone and the stream is deallocated while still in use, resulting in a crash.To avoid the problem, ensure that your block maintains a strong reference to the stream by using a locally-scoped variable instead of an ivar:
Note that creating a dispatch queue here for this one block is not meaningful; you should just use the global queue.