在Cocoa中解决EXC_BAD_ACCESS问题? [英] Resolving EXC_BAD_ACCESS Issue In Cocoa?

查看:86
本文介绍了在Cocoa中解决EXC_BAD_ACCESS问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



- (void)startUploadWithContainerName:(NSString *)containerName
{
//创建一个NSFileManager对象并获取本地文件夹内容和云文件夹内容的数组
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(@\\\
%@ \\\
\\\
%@,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(@\\\
SDiff 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:@filefile:uploadPath metadata:nil etag:nil];
[request startSynchronous];
NSLog(@已上传%@,foundPath);
}



}
else {
NSLog(@在云中找不到:%@,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:@filefile:uploadPath metadata:nil etag:nil];
[request startSynchronous];
NSLog(@上传完成);
}
}
[uploadPath release];

[cloudList release];
[uploadFolderContents release];

}

但它挂起显示异常


已接收的讯号EXC_BAD_ACCESS


任何人都可以清除问题吗?异常发生在NSLog(@Found In Cloud:%@,foundPath);

解决方案

通常,您将环境变量NSZombieEnabled设置为YES,然后解决问题。
在你的情况下,我看到你已经声明指针,而不指向任何危险的对象。在Clang Analyzer中运行代码会将其报告为警告。
将这些指针设置为nil。你已经声明了字符串指针,但是在你的for循环中如果if不是true,那么它会去else where foundPath永远不会指向任何东西,你试图访问它if(i == 1)



更新:
也请考虑Lou Franco的回答。他也是正确的。您没有cloudList对象。它是自动释放的,你是通过传递释放消息到cloudList对象[cloudList版本]过度释放它。
在你的情况下,它可能不会立即崩溃,当你释放它,因为控制在同一个循环。一旦当前线程自动释放池耗尽,您的代码将与EXC_BAD_ACCESS崩溃。

  ASICloudFilesObjectRequest * cloudList = [ASICloudFilesObjectRequest listRequestWithContainer:containerName]; 
[cloudList startSynchronous];
NSArray * cloudFolderContents = [cloudList objects];
[cloudList release]; //删除此行

Update2: strong>

  NSString * uploadPath = [[[NSString alloc] initWithString:@〜/ Cloud Briefcase] stringByExpandingTildeInPath]; 

将上述更改为

  NSString * uploadPath = [[NSString stringWithFormat:@〜/ Cloud Briefcase] stringByExpandingTildeInPath]; 

uploadPath在上面的行仍指向自动释放的对象。你泄漏一个你创建的字符串。调用释放是错误的。所以删除[uploadPath发布]和[cloudlist发布]你一次又一次地释放它。为什么你发布显式autoreleased对象uploadFolderContents?从您的代码中删除这三行:

  [uploadPath release]; 

[cloudList release];
[uploadFolderContents release];

Update3:修复了过度发布问题。并且updatePath in if块更改为updatePathLocal,与updatePath变量与方法范围发生冲突。

   - (void)startUploadWithContainerName :NSString *)containerName 
{
//创建NSFileManager对象并获取本地文件夹内容和云文件夹内容的数组
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(@\\\
%@ \\\
\\\
%@,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(@\\\
SDiff 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:@filefile:uploadPath metadata:nil etag:nil];
[request startSynchronous];
NSLog(@已上传%@,foundPath);
}
}
else {
NSLog(@在云中找不到:%@,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:@filefile:uploadPathLocal metadata:nil etag:nil];
[request startSynchronous];
NSLog(@上传完成);
}
}
}



Hey..i have the following method in cocoa..

-(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];

 }

But it hangs showing the exception

Received Signal EXC_BAD_ACCESS

Can anyone clear the issue out? the exception occurs at NSLog(@"Found In Cloud: %@",foundPath);

解决方案

Typically you would set the environment variable NSZombieEnabled to YES and then troubleshoot the issue. In your case I see that you have declared pointers without pointing to any object which is dangerous. Running your code in Clang Analyzer would report it as warning. Set those pointers to nil. You have declared string pointer but in your for loop if the "if" is not true then it goes to else where foundPath is never pointed to anything and you tried to access it in if(i==1)

Update: Also consider Lou Franco 's answer. He is correct too. You don't own cloudList object. It is autoreleased and you are over-releasing it by passing release message to cloudList object [cloudList release]. In your case it might not crash instantly when you release it because control is in same loop. Once the current threads autorelease pool is drained your code will crash with 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];

Change the above to

NSString *uploadPath=[[NSString stringWithFormat:@"~/Cloud Briefcase"] stringByExpandingTildeInPath];

uploadPath in above line still points to autoreleased object. You are leaking a string you have created. Calling release is wrong. So remove [uploadPath release] and also [cloudList release] you are releasing it again and again. And why are you releasing obviously autoreleased object uploadFolderContents ? remove these three lines from your code:

[uploadPath release];

[cloudList release];
[uploadFolderContents release];

Update3: Fixed over-release issues. And updatePath in if block changed to updatePathLocal where there is a conflict with updatePath variable with scope of method.

-(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");
    }
 }
}

这篇关于在Cocoa中解决EXC_BAD_ACCESS问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆