2011-08-31 46 views
0
NSError *theError = nil; 
NSArray *keys = [NSArray arrayWithObjects:@"password", @"userId", nil]; 
NSArray *objects = [NSArray arrayWithObjects:passwordTextField.text, userNameTextField.text, nil]; 
NSDictionary *requestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

NSString *JSONString =[requestDictionary JSONRepresentation]; 
NSData *JSONData =[JSONString dataUsingEncoding:NSUTF8StringEncoding]; 
NSLog(@"JSONString :%@", JSONString); 
NSLog(@"JSONData :%@", JSONData); 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://153.20.32.74/11AprP306/passenger/jsonitem"]]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:JSONData]; 
[request setHTTPMethod:@"POST"]; 

NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError]; 
NSLog(@"response : %@", theResponse); 
NSLog(@"error : %@", theError); 
NSLog(@"data : %@", data); 
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSLog(@"string: %@", string); 
[string release]; 
//[theResponse release]; // this statement crashes the app 

有它得到的東西與做這樣的說法:NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError]; 我看到使用的&象徵。這是什麼意思?爲什麼發佈聲明在這裏崩潰了應用程序?

回答

1

好了,你自動釋放theResponse當實例,所以釋放它兩次是造成你的問題。要麼不要撥打autorelease電話,要麼不要撥打release電話。

個人而言,我擺脫autorelease的。 release爲您的程序運行提供了更好的控制。

哦,&沒有什麼可擔心的 - 它只是通過它所進行變量的地址。在這種情況下,您需要通過NSURLResponse**。由於您有NSURLResponse*,因此您將參考文獻傳遞給它。

+0

對不起,自動釋放陳述並沒有想在那裏。我加入來解決問題。 – John

1

這是因爲theResponse已發送的消息autorelease

NSURLResponse *theResponse =[[[NSURLResponse alloc]init] autorelease]; 

如果releaseautoreleased您會導致應用程序崩潰的垃圾收集器會在釋放對象的對象。

&只是意味着「給我的theErrortheResponse(基本上要傳遞指針的一個指針,它是由該方法sendSynchronousRequest:returningResponse:error:必需)

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request 
       returningResponse:(NSURLResponse **)response 
          error:(NSError **)error 

NSURLResponse **NSError **手段「地址的地址的地址'所以給他們只有theErrortheResponse(沒有&)只會在預期的時候給他們的方法'他們的地址'

+0

對不起,autorelease聲明不假設在那裏。我加入來解決問題 – John

+0

我只是想知道爲什麼如果我手動調用release方法,它會崩潰。 – John

+0

好吧我明白,你做錯了我會發布正確的答案,但你應該提到你已經編輯了你的問題本身的問題,因爲現在,有人進入你的問題不會理解我們以前的答案 – apouche

3

I'l l當你編輯你的問題時,將其作爲新的答案發布。

你這樣做是錯誤的。

這是sendSynchronousRequest:returningResponse:error:的責任來爲您創建的響應(或錯誤,如果出事了)

這就是爲什麼你需要傳遞一個指針theResponse和指向theError

當撥打sendSynchronousRequest:returningResponse:error:已完成,theResponse將被創建,最重要的是autoreleased(通過sendSynchronousRequest:returningResponse:error)!

所以最後你回到了autorelease/over release問題。

正確的代碼是:

NSURLResponse *theResponse = nil; // no need to init it will be done later on 
NSError *theError = nil; // no need to init either 
NSData *data = [NSURLConnection sendSynchronousRequest:request 
            returningResponse:&theResponse 
               error:&theError]; 

if (aError != nil) { } // handle error 
else {} // handle your response data 

//no need to release theResponse