2011-03-02 5 views
1

문자열을 디렉토리 경로로 반환하는 함수를 작성했습니다. EXC_BAD_ACCESS가 표시되며이 함수가 원인이라고 생각합니다. nstring 또는 다른 것을 유지해야합니까?NSString을 할당 할 때 EXC_BAD_ACCESS 가져 오기

-(void) getRemoteFiles:(NSMutableArray *) M 
{ 
    [self createFileToAppDirectory]; 

    if (!networkqueue) { 
     networkqueue:[[[ASINetworkQueue alloc] init] autorelease]; 
    } 

    [[self networkQueue] cancelAllOperations]; 

    [self setNetworkQueue:[ASINetworkQueue queue]]; 
    [[self networkQueue] setDelegate:self]; 
    [[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)]; 
    [[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)]; 
    [[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)]; 


    int i; 

    for (i=0; i<[M count]; i++) { 
     NSString *url=[M objectAtIndex:i]; 
     NSString* theFileName = [url lastPathComponent]; 
     NSString *safestring=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     if ([theFileName isEqualToString:@"nothing"]==NO) { 

      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:safestring]]; 
      //think this is causing the problem 
      NSString *savepath=[self getDirectoryPathForFileName:theFileName]; 
      //[request setDownloadDestinationPath:savepath]; 
      [[self networkQueue] addOperation:request]; 
     } 

    } 

    [[self networkQueue] go]; 
    //error thrown after this point 


} 
-(NSString *)getDirectoryPathForFileName:(NSString *)filename 

{ 
    NSFileManager *filemgr; 
    NSArray *dirPaths; 
    NSString *docsDir; 
    NSString *newDir; 
    BOOL isDir; 


    filemgr =[NSFileManager defaultManager]; 
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    docsDir = [dirPaths objectAtIndex:0]; 

    newDir = [docsDir stringByAppendingPathComponent:@"remix_data"]; 
    if ([filemgr fileExistsAtPath:newDir isDirectory:&isDir]==NO) { 
     NSLog(@"dir should exist but does not... go funt dat"); 
    } 

    NSString *localFilePath = [newDir stringByAppendingPathComponent:filename]; 
    [filemgr release]; 
    [docsDir release]; 
    [newDir release]; 
    return localFilePath; 
} 
+0

filemgr, docsDir 및 newDir에서 alloc/init을 수행하지 않습니다. 그럼 왜 그걸 풀어주는거야? –

+0

나는 여전히 메모리 관리에 대해 배우고 있으며 코드는 예제에서 나온 것이다. 나는 그 예가 잘못되었다고 생각하니? – dubbeat

답변

2

아니요, 사실 과장입니다. getDirectoryPathForFileName에서는 결코 그것을 보관하지 않았지만 docsDir (예 :)을 릴리스합니다. 당신은 dirPaths 배열에서 그것을 꺼내고 있었고, 보유 수는 증가하지 않습니다. 이것을 릴리즈하면 retain 카운트가 0으로 줄어들어 어레이가 여전히 그것을 보유하고 있다고 생각하는 동안 문자열을 dealloc하게됩니다. 다음 번 배열에서 해당 문자열로 작업을 수행하면 앱이 다운됩니다. docsDir 및 newDir에 대한 릴리스 호출을 제거하면 좋을 것입니다 ...

+0

나는 이것을 시도 할 것이다. nsstring을 사용하는 것이 괜찮은 것 같습니까? – dubbeat

+0

네, 저에게 완벽하게 합당한 것처럼 보입니다. 필요하지 않거나 잘못 사용하는 경우는 없습니다. 당신은 그 메모리 관리 문제에 익숙해 져야하지만 NSString과 관련이 없습니다 ... 두려워하지 마세요. 익숙해 질 것입니다! 경험 법칙 : init로 시작하는 메소드를 사용하여 작성한 경우 또는 수동으로 보유한 경우 무언가를 릴리스하면됩니다. – Toastor

+0

해결책이 정확하고 설명이 도움이되고 – dubbeat

-1

예 문자열을 유지하고 사용 후에 해제하는 것이 좋습니다. docsDirnewDir 문자열도 할당하지 않기 때문에이 문자열을 릴리스하지 마십시오. 그것들은 자동 릴리즈 객체입니다.

1

언제나 누군가가 EXC_BAD_ACCESS 문제가있는 경우 NSZombie를 권장합니다. 당신의 특별한 경우에는 왜 충돌 하는지를 쉽게 알 수 있습니다. 왜냐하면 당신은 alloc'd를 갖지 않고 물건을 풀어 주거나 ​​그것을 먼저 유지하기 때문입니다. 당신은 당신이 alloc'd했거나 자신을 유지 물건을 공개해야합니다. 다음을 수행

이 NSZombie를 활성화하려면 :

  1. 은의 정보를 얻을

    당신이 미래에 EXC_BAD_ACCESS 문제를 얻을 경우

    , 즉 여기 NSZombie를 사용하는 방법, 파악하기 쉽지 않다 실행 파일.

  2. 인수 탭으로 이동하십시오. 섹션 추가하십시오에서
  3. "변수는 환경에 설정되는"

이름 : 값을 NSZombieEnabled : YES

그런 다음 평소와 같이 귀하의 응용 프로그램을 실행하고 충돌 할 때 당신에게 말할해야하는 할당 해제 된 객체가 해제 메시지를 수신했습니다.

+1

그리고 나중에 좀비를 비활성화하는 것을 잊지 마십시오. 당신은 NSZombies를 떠났다는 것을 알아 내기 위해 30 분 동안 누출을 사냥하고 싶지 않습니다. 어제처럼. –

+0

좋은 지적입니다. –

관련 문제