Skip to content
Browse files

Fixed memory management issue in latest changes to ubiquity monitor. …

…The autorelease pool was releasing the returned NSError objects before they could be logged out, causing a crash.
  • Loading branch information...
1 parent 8c07881 commit b1a2771cbc4c94bbf5113bce7639b6278d230bc6 @drewmccormack committed Mar 9, 2013
Showing with 24 additions and 10 deletions.
  1. +24 −10 TICoreDataSync/06 External Sources/TIUbiquityMonitor.m
View
34 TICoreDataSync/06 External Sources/TIUbiquityMonitor.m
@@ -123,7 +123,7 @@ - (void)update:(NSNotification *)notif
NSError *downloadError = nil, *uploadError = nil;
NSUInteger count = [metadataQuery resultCount];
- long long toDownload = 0, toUpload = 0;
+ __block long long toDownload = 0, toUpload = 0;
for ( NSUInteger i = 0; i < count; i++ ) {
@autoreleasepool {
NSURL *url = [metadataQuery valueOfAttribute:NSMetadataItemURLKey forResultAtIndex:i];
@@ -141,27 +141,41 @@ - (void)update:(NSNotification *)notif
toDownload += fileDownloadSize;
// Start download
- if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&downloadError] ) downloadErrorArose = YES;
+ NSError *error;
+ if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&error] ) {
+ if ( downloadErrorArose ) [downloadError release]; // Release old error
+ downloadErrorArose = YES;
+ downloadError = [error retain];
+ }
}
else if ( uploaded && !uploaded.boolValue ) {
double percentage = percentUploaded ? percentUploaded.doubleValue : 0.0;
long long fileDownloadSize = (1.0 - percentage / 100.0) * fileSize;
toUpload += fileDownloadSize;
// Force upload
- if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&uploadError] ) uploadErrorArose = YES;
+ NSError *error;
+ if ( initiateTransfers && percentage < 1.e-6 && ![fileManager startDownloadingUbiquitousItemAtURL:url error:&error] ) {
+ if ( uploadErrorArose ) [uploadError release];
+ uploadErrorArose = YES;
+ uploadError = [error retain];
+ }
}
}
}
+ // Log last error
+ if ( downloadErrorArose ) {
+ NSLog(@"Failed to initiate download(s) with last error: %@", downloadError);
+ }
+ if ( uploadErrorArose ) {
+ NSLog(@"Failed to initiate download(s) with last error: %@", uploadError);
+ }
+ [downloadError release];
+ [uploadError release];
+
+ // Update on main queue, and callback
dispatch_async(dispatch_get_main_queue(), ^{
- if ( downloadErrorArose ) {
- NSLog(@"Failed to initiate download(s) with last error: %@", downloadError);
- }
- if ( uploadErrorArose ) {
- NSLog(@"Failed to initiate download(s) with last error: %@", uploadError);
- }
-
ubiquitousBytesToDownload = toDownload;
ubiquitousBytesToUpload = toUpload;

0 comments on commit b1a2771

Please sign in to comment.
Something went wrong with that request. Please try again.