2011-10-07 49 views
1

Hey..i在可可下面的方法..解决可可中的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 *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]; 

} 

但它挂出异常

接收信号EXC_BAD_ACCESS

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

回答

2

通常,您可以将环境变量NSZombieEnabled设置为YES,然后解决问题。 在你的情况,我看到你已经声明指针而不指向任何危险的对象。在Clang Analyzer中运行您的代码会将其报告为警告。 将这些指针设置为nil。您已声明的字符串指针,但你如果“如果”是不正确的,然后它去到别的for循环,其中foundPath是从来没有指出任何东西,你试图访问它,如果(我== 1)

更新: 也考虑娄佛朗哥的答案。他也是对的。您不拥有cloudList对象。它是自动发布的,并且通过将发布消息传递给cloudList对象[cloudList发布]来过度释放它。 在你的情况下,它可能不会立即崩溃当你释放它,因为控制在同一个循环。一旦当前的线程自动释放池被耗尽,你的代码将崩溃并伴随着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仍然指向自动释放物体。您正在泄漏您创建的字符串。呼叫释放是错误的。因此,请删除[uploadPath发布]和[cloudList发布],一次又一次地发布它。为什么你会发布明显自动发布的对象uploadFolderContents?从代码中删除以下三行:

[uploadPath release]; 

[cloudList release]; 
[uploadFolderContents release]; 

UPDATE3:修正过放的问题。如果block中的updatePath更改为updatePathLocal,其中up​​datePath变量与方法范围发生冲突。

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

它解决了例外在于method..but它导致相同的异常在main.m文件 #进口<可可/ Cocoa.h> INT主(INT的argc,字符* argv的[]){ return NSApplicationMain(argc,(const char **)argv); } 返回NSApplica .....线似乎已经捕捉到的异常.. – proctr

+0

@twistedcerebrum考虑重构你的代码,请。方法的本地变量与变量本地的相同名称与if块或块的名称相同。使我们难以阅读您的代码。你的应用崩溃了什么? EXC_BAD_ACCESS?当你在调试模式下运行断点时,它会显示控制停在main方法中,它的正常并不意味着你需要修复main方法。它的原因是你捣毁了堆栈,所以它找不到当前函数的框架,或者它实际上停在另一个线程中。 – 0x8badf00d

+0

好man..made sense..thnx .. – proctr

1

这条线:

[cloudList release]; 

是犯罪嫌疑人,因为你没有页头或保留cloudList - 它很可能返回自动释放,但阅读ASICloudFilesObjectRequest的文档。

即便如此,我不认为这是你的问题 - 一旦它被过度发售,它会导致问题。

我看到很多版本似乎不好 - 因为我没有看到相应的分配或保留。你需要读取内存管理的Objective-C的规则(或移动到自动引用计数)

我记录的调试技术EXC_BAD_ACCESS这里:

http://loufranco.com/blog/files/Understanding-EXC_BAD_ACCESS.html

容易的事情尝试:

  1. 运行的建立与分析(解决所有问题)
  2. 打开僵尸
0

也许,只是可能“foundPath”未设置为有效值。有一个调试器,也许你应该使用它。

+0

没有找到路径设置为一个有效的value..it已经验证earlier..nd除了,,,当我评论该行...同样的错误转移到下一行.. – proctr

+0

呃!下一行也引用“foundPath”。 PYHOOYA! –

+0

是的..但foundPath验证.. – proctr

0

这可能是引用计数问题。

诊断:

  • 运行静态分析
  • 修复所有静态分析仪固定静态分析结果后发出
  • 测试

仍然有问题?没问题:

  • 在Instruments中,您可以启用僵尸检测并记录分配历史记录和引用计数。
  • 运行您的应用程序,执行重新发布问题所需的步骤
  • 仪器将在发送僵尸消息时停止执行。
  • 然后找到已分配消息的分配/对象,并遍历其生存期/参考计数历史。

这使得定位过度释放的对象非常容易找到(例如从某些情况下的几小时到几分钟)。