0

테스트 요청이 발생하고 공식 사과 개발자 사이트에서 응답 예제가 표시됩니다.connectionDidFinishLoading의 연결 해제 방법으로 인해

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

connectionDidFinishLoading에서 [연결 해제] 방법은 오류가 발생하여이 : 무효화 했어야 - EXC_BAD_ACCESS

내가 라인을 코멘트 경우 [연결 해제] 방법; 모든 것이 작동하는 것처럼 보이지만 존재하지 않는 연결 해제로 인해 메모리 누수가 발생합니다.

이 문제를 방지하려면 어떻게해야합니까?

@imlementation test 
NSMutableData *dataStore=nil; 

//example usage: 
//[self registerToServer:@"http://testserver.com/registeruser.ashx" withUserName:@"john doe" withPassword:@"123456"]; 

-(void)registerToServer:(NSString*)urlstr withUserName:(NSString*) 
ausername withPassword:(NSString*)apassword 
{ 
    NSURL *url=[NSURL URLWithString:urlstr]; 
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:3.0f]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:ausername forHTTPHeaderField:@"pass"]; 
[request setValue:apassword forHTTPHeaderField:@"username"]; 
NSURLConnection *connection=[NSURLConnection connectionWithRequest:request delegate:self]; 
[connection start]; 

if(connection) 
    dataStore=[[NSMutableData data]retain]; 
} 


- (무효) 연결 (있는 NSURLConnection *) 연결 didReceiveData (을 NSData *) 데이터 {[데이터 저장소에 대한 appendData : 데이터]; }

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
[dataStore setLength:0];} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
NSLog(@"connection failed:%@ %@", 
     [error localizedDescription], 
     [[error userInfo]objectForKey:NSURLErrorFailingURLStringErrorKey]); 

[connection release]; 
[dataStore release]; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
    //[connection release]; //WELL... I CANT COMMENT OUT THIS LINE! 
    NSString *res=[[NSString alloc]initWithData:dataStore encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@",res); 
[dataStore release]; 
    } 
+0

NSURLConnection 인스턴스를 어떻게 만듭니 까? –

+0

NSURLConnection 인스턴스 생성으로 질문이 업데이트되었습니다. 간단한 CustomNSObject의 싱글 톤 인스턴스 사용 감사합니다. –

답변

0

당신은 팩토리 메소드를 사용하여 연결을 만듭니다. alloc/init, new, copy 또는 retain을 사용해야하는 경우에만 release을 사용해야합니다. 이 경우 시스템이 개체를 릴리스 처리합니다.

더 나은 ARC를 사용하십시오.