Objective c 解决Cocoa中的EXC\u坏访问问题?
嘿..我有下面的可可方法Objective c 解决Cocoa中的EXC\u坏访问问题?,objective-c,xcode,macos,cocoa,exc-bad-access,Objective C,Xcode,Macos,Cocoa,Exc Bad Access,嘿..我有下面的可可方法 -(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 *
-(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 alloc] initWithString:@"~/Cloud Briefcase"] stringByExpandingTildeInPath];
NSError *err;
NSArray *uploadFolderContents=[uploadManager contentsOfDirectoryAtPath:uploadPath error:&err];
ASICloudFilesObjectRequest *cloudList = [ASICloudFilesObjectRequest listRequestWithContainer:containerName];
[cloudList startSynchronous];
NSArray *cloudFolderContents = [cloudList objects];
[cloudList release];
[uploadManager release];
NSLog(@"%lu",[uploadFolderContents count]);
NSLog(@"\n%@\n\n%@",cloudFolderContents,uploadFolderContents);
NSString *notFoundPath;
NSString *foundPath;
NSString *foundCloudMatch;
NSDate *cloudUploadDate;
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 *uploadPath=[[NSString stringWithFormat:@"~/Cloud Briefcase/%@",foundPath] stringByExpandingTildeInPath];
NSTimeZone *tCST=[NSTimeZone timeZoneWithAbbreviation:@"CST"];
NSInteger cloudDifference=[tCST secondsFromGMTForDate:cloudUploadDate];
NSFileManager *typeManager=[[NSFileManager alloc] init];
NSError *Er;
NSDictionary *propertiesOfUploadFile=[typeManager attributesOfItemAtPath:uploadPath 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);
[localUploadDate release];
[propertiesOfUploadFile release];
[typeManager release];
[tCST 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 *uploadPath=[[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:uploadPath metadata:nil etag:nil];
[request startSynchronous];
NSLog(@"Upload Complete");
}
}
[uploadPath release];
[cloudList release];
[uploadFolderContents release];
}
-(void)startUploadWithContainerName:(NSString*)containerName
{
//创建NSFileManager的对象并获取本地文件夹内容和云文件夹内容的数组
NSFileManager*uploadManager=[[NSFileManager alloc]init];
NSString*uploadPath=[[NSString alloc]initWithString:@“~/Cloud公文包”]stringByExpandingTildeInPath];
n错误*错误;
NSArray*uploadFolderContents=[uploadManager内容目录路径:uploadPath错误:&err];
AsiCloudFileObjectRequest*cloudList=[AsiCloudFileObjectRequestListRequestWithContainer:containerName];
[云列表启动同步];
NSArray*cloudFolderContents=[cloudList对象];
[云名单发布];
[上传管理器发布];
NSLog(@“%lu”,[uploadFolderContents count]);
NSLog(@“\n%@\n\n%@”、cloudFolderContents、uploadFolderContents);
NSString*notFoundPath;
NSString*foundPath;
NSString*foundCloudMatch;
NSDate*cloudUploadDate;
对于(int j=1;j,通常将环境变量NSZombieEnabled设置为YES,然后对问题进行故障排除。
在您的例子中,我看到您声明的指针没有指向任何危险的对象。在Clang Analyzer中运行代码会将其报告为警告。
将这些指针设置为nil。您已经声明了字符串指针,但在for循环中,如果“if”不为true,那么它将转到else,其中foundPath从未指向任何对象,并且您尝试在if(i==1)中访问它
更新:
也可以考虑Lou Franco的回答。他也是正确的。你不拥有CyrdListObjor。它是自动恢复的,并且通过将发布消息传递给CyrdListObj[CurrdList]版本,将其释放。
在您的情况下,当您释放它时,它可能不会立即崩溃,因为控制在同一个循环中。一旦当前线程自动释放池耗尽,您的代码将在EXC\u BAD\u访问时崩溃
ASICloudFilesObjectRequest *cloudList = [ASICloudFilesObjectRequest listRequestWithContainer:containerName];
[cloudList startSynchronous];
NSArray *cloudFolderContents = [cloudList objects];
[cloudList release];// Remove this line
更新2:
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:修复了版本问题。如果块更改为updatePath局部,则updatePath中的updatePath与方法范围内的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");
}
}
}
-(void)startUploadWithContainerName:(NSString*)containerName
{
//创建NSFileManager的对象并获取本地文件夹内容和云文件夹内容的数组
NSFileManager*uploadManager=[[NSFileManager alloc]init];
NSString*uploadPath=[[NSString stringWithFormat:@“~/Cloud公文包”]stringByExpandingTildeInPath];
n错误*err=nil;
NSArray*uploadFolderContents=[uploadManager内容目录路径:uploadPath错误:&err];
AsiCloudFileObjectRequest*cloudList=[AsiCloudFileObjectRequestListRequestWithContainer:containerName];
[云列表启动同步];
NSArray*cloudFolderContents=[cloudList对象];
[上传管理器发布];
NSLog(@“%lu”,[uploadFolderContents count]);
NSLog(@“\n%@\n\n%@”、cloudFolderContents、uploadFolderContents);
NSString*notFoundPath=nil;
NSString*foundPath=nil;
NSString*foundCloudMatch=nil;
NSDate*cloudUploadDate=nil;
对于(int j=1;j这条线:
[cloudList release];
是可疑的,因为您没有分配或保留cloudList——它可能会作为自动释放返回,但请阅读ASICloudFilesObjectRequest的文档
即便如此,我也不认为这是你的问题——一旦它被过度夸大,以后就会引起问题
我看到很多版本看起来很糟糕,因为我没有看到相应的alloc或reserves。您需要阅读Objective-C的内存管理规则(或者转到自动引用计数)
我在这里记录了EXC_BAD_访问的调试技术:
简单的尝试:
运行构建和分析(修复所有问题)
打开僵尸
可能,只是可能“foundPath”未设置为有效值。有一个调试器,可能您应该使用它。这可能是引用计数问题
要诊断:
- 运行静态分析器
- 修复所有静态分析器问题
- 试验
修复静态分析仪结果后仍有问题?无问题:
- 在仪器中,您可以启用僵尸检测并记录分配历史和参考计数
- 运行应用程序,执行重新设置问题所需的步骤
- 当发送僵尸消息时,工具将停止执行
- 然后找到发送消息的分配/对象,并逐步查看其生存期/引用计数历史记录
这使得查找已发布对象非常容易(例如,在某些情况下,从小时到分钟)。否找到的路径设置为有效值..已在前面进行了验证..nd此外,,,当我注释该行时…相同的错误转移到下一行..Duh!!下一行还引用了“foundPath”.PYHOOYA!我最初在一个粗略的项目中运行了代码,并记录了foundPath的值,它显示了正确的值。此外,问题已经用上面接受的解决方案解决了。无论如何,谢谢。调试器是知识的交流。谢谢!它用该方法解决了异常。但它在main.m文件中导致了相同的异常#导入int main(int argc,char*argv[]){返回NSApplicationMain(argc,(const char**)argv);}返回NSAPPLA...…行似乎已经遇到了例外。@ TouthDistCurm请考虑重构您的代码。方法局部变量具有与本地变量相同的名称,如果是块或块。我们很难读取代码。您的应用程序与什么?ExcdBADiAccess发生冲突?如果在其上重新设置,则显示控制已在主方法中停止