通常,您会将环境变量 NSZombieEnabled 设置为 YES,然后解决问题。
在你的例子中,我看到你声明了指针,但没有指向任何危险的对象。在 Clang 分析器中运行代码会将其报告为警告。
将这些指针设置为零。您已经声明了字符串指针,但在 for 循环中,如果“if”不为 true,则它会转到 else ,其中 findPath 从未指向任何内容,并且您尝试在 if(i==1) 中访问它
Update:另请考虑 Lou Franco 的答案。他也是对的。您不拥有 cloudList 对象。它是自动释放的,并且您通过将释放消息传递给 cloudList 对象 [cloudList release] 来过度释放它。
在您的情况下,当您释放它时,它可能不会立即崩溃,因为控制位于同一循环中。一旦当前线程自动释放池耗尽,您的代码将因 EXC_BAD_ACCESS 崩溃。
ASICloudFilesObjectRequest *cloudList = [ASICloudFilesObjectRequest listRequestWithContainer:containerName];
[cloudList startSynchronous];
NSArray *cloudFolderContents = [cloudList objects];
[cloudList release];// Remove this line
Update2:
NSString *uploadPath=[[[NSString alloc] initWithString:@"~/Cloud Briefcase"] stringByExpandingTildeInPath];
将上面的内容改为
NSString *uploadPath=[[NSString stringWithFormat:@"~/Cloud Briefcase"] stringByExpandingTildeInPath];
上面一行中的 uploadPath 仍然指向自动释放的对象。您正在泄漏您创建的字符串。调用release是错误的。因此,删除 [uploadPath release] 和 [cloudList release],您将一次又一次地发布它。为什么你要释放明显自动释放的对象 uploadFolderContents ?从代码中删除这三行:
[uploadPath release];
[cloudList release];
[uploadFolderContents release];
Update3:修复了过度释放的问题。 if 块中的 updatePath 更改为 updatePathLocal,其中 updatePath 变量与方法范围存在冲突。
-(void)startUploadWithContainerName:(NSString *)containerName
{
//Make an object of NSFileManager and Fetch an array of local folder contents and cloud folder contents
NSFileManager *uploadManager=[[NSFileManager alloc] init];
NSString *uploadPath=[[NSString stringWithFormat:@"~/Cloud Briefcase"] stringByExpandingTildeInPath];
NSError *err = nil;
NSArray *uploadFolderContents=[uploadManager contentsOfDirectoryAtPath:uploadPath error:&err];
ASICloudFilesObjectRequest *cloudList = [ASICloudFilesObjectRequest listRequestWithContainer:containerName];
[cloudList startSynchronous];
NSArray *cloudFolderContents = [cloudList objects];
[uploadManager release];
NSLog(@"%lu",[uploadFolderContents count]);
NSLog(@"\n%@\n\n%@",cloudFolderContents,uploadFolderContents);
NSString *notFoundPath = nil;
NSString *foundPath = nil;
NSString *foundCloudMatch = nil;
NSDate *cloudUploadDate = nil;
for (int j=1; j<[uploadFolderContents count]; j++) {
int i=0;
for (int k=0; k<[cloudFolderContents count]; k++) {
if ([[[cloudFolderContents objectAtIndex:k] name] isEqualToString:[uploadFolderContents objectAtIndex:j]]) {
i=1;
foundPath=[uploadFolderContents objectAtIndex:j];
foundCloudMatch=[cloudFolderContents objectAtIndex:k];
cloudUploadDate=[[cloudFolderContents objectAtIndex:k] lastModified];
break;
}
else{
i=0;
notFoundPath=[uploadFolderContents objectAtIndex:j];
continue;
}
}
if (i==1) {
NSLog(@"Found In Cloud: %@",foundPath);
NSString *uploadPathLocal=[[NSString stringWithFormat:@"~/Cloud Briefcase/%@",foundPath] stringByExpandingTildeInPath];
NSTimeZone *tCST=[NSTimeZone timeZoneWithAbbreviation:@"CST"];
NSInteger cloudDifference=[tCST secondsFromGMTForDate:cloudUploadDate];
NSFileManager *typeManager=[[NSFileManager alloc] init];
NSError *Er = nil;
NSDictionary *propertiesOfUploadFile=[typeManager attributesOfItemAtPath:uploadPathLocal error:&Er];
NSDate *localUploadDate=[propertiesOfUploadFile objectForKey:NSFileModificationDate];
NSInteger sourceUploadDifference=[[NSTimeZone systemTimeZone] secondsFromGMTForDate:localUploadDate];
NSLog(@"Local Date %@",localUploadDate);
NSLog(@"Local Difference %ld",sourceUploadDifference);
NSTimeInterval diff=sourceUploadDifference-cloudDifference;
NSTimeInterval sDiff=sourceUploadDifference;
NSDate *lDate=[[NSDate alloc] initWithTimeInterval:sDiff sinceDate:localUploadDate];
NSDate *comparisonDate=[[NSDate alloc] initWithTimeInterval:diff sinceDate:cloudUploadDate];
NSLog(@"\nSDiff Value %@",lDate);
NSLog(@"Comparison Date %@",comparisonDate);
[typeManager release];
if ([comparisonDate compare:lDate]==NSOrderedAscending) {
[comparisonDate release];
[lDate release];
NSLog(@"Got It");
NSString *escString=[foundPath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
ASICloudFilesObjectRequest *request =
[ASICloudFilesObjectRequest putObjectRequestWithContainer:containerName objectPath:escString contentType:@"file" file:uploadPath metadata:nil etag:nil];
[request startSynchronous];
NSLog(@"Uploaded %@",foundPath);
}
}
else{
NSLog(@"Not Found In Cloud: %@",notFoundPath);
NSString *uploadPathLocal=[[NSString stringWithFormat:@"~/Cloud Briefcase/%@",notFoundPath] stringByExpandingTildeInPath];
// NSLog(@"%@",uploadPath);
NSString *escString=[notFoundPath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"URL ENCODED VALUE: %@",escString);
ASICloudFilesObjectRequest *request =
[ASICloudFilesObjectRequest putObjectRequestWithContainer:containerName objectPath:escString contentType:@"file" file:uploadPathLocal metadata:nil etag:nil];
[request startSynchronous];
NSLog(@"Upload Complete");
}
}
}