2011-11-13 14 views
0

我在執行NSURLConnection委託方法connectionDidFinishLoading時收到錯誤。有趣的是,它可以在模擬器上運行,但它在物理設備上崩潰。更有趣的是它崩潰,只有當這種操作序列完成後,在connectionDidFinishLoading中收到錯誤,EXC_BAD_ACCESS

  • 運行應用程序
  • 顯示資料Tweet! (真棒)
  • 按Home鍵
  • 雙擊Home鍵
  • 強制退出應用程序
  • 再次打開應用
  • CRASHED !!!!! (:()...保持崩潰,直到你重新啓動手機

錯誤日誌

Exception Type: EXC_BAD_ACCESS (SIGSEGV) 
Exception Codes: KERN_INVALID_ADDRESS at 0xa0000008 
Crashed Thread: 0 

Thread 0 name: Dispatch queue: com.apple.main-thread 
Thread 0 Crashed: 
0 libobjc.A.dylib     0x321befbc 0x321bb000 + 16316 
1 Tittle-Tattle     0x0002cf10 -[MapViewController connectionDidFinishLoading:] (MapViewController.m:108) 
2 Foundation      0x3316bc32 0x330a5000 + 814130 
3 Foundation      0x330c36e2 0x330a5000 + 124642 
4 Foundation      0x330c36ac 0x330a5000 + 124588 
5 Foundation      0x330c35ce 0x330a5000 + 124366 
6 CFNetwork      0x35e7689e 0x35e67000 + 63646 
7 CFNetwork      0x35e6b53e 0x35e67000 + 17726 
8 CFNetwork      0x35e6b632 0x35e67000 + 17970 
9 CFNetwork      0x35e6b23c 0x35e67000 + 16956 
10 CFNetwork      0x35e6b172 0x35e67000 + 16754 
11 CoreFoundation     0x34176afc 0x340e9000 + 580348 
12 CoreFoundation     0x341762c8 0x340e9000 + 578248 
13 CoreFoundation     0x3417506e 0x340e9000 + 573550 
14 CoreFoundation     0x340f84d6 0x340e9000 + 62678 
15 CoreFoundation     0x340f839e 0x340e9000 + 62366 
16 GraphicsServices    0x3254dfc6 0x3254a000 + 16326 
17 UIKit       0x3734e73c 0x3731d000 + 202556 
18 Tittle-Tattle     0x000200e0 main (main.m:16) 
19 Tittle-Tattle     0x00020084 start + 32 

CODE

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    [urlConnection cancel]; 
    [urlConnection release]; 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

    //Since we got new fresh data we shall put it in LatestLocationData entity in CoreData 
    [self insertLastKnownLocationDataIntoCoreDataWith:responseString]; 

    //Test purpose only, See what we have got in CoreData 
    [self fetchLastKnownLocationDataFromCoreData]; 

    NSDictionary *results = [responseString JSONValue]; 
    placesNearBy = [results objectForKey:@"results"]; 

    [responseString release]; 

    [self dropNearByPlacesAnnotationsFrom:placesNearBy]; 

} 

問:什麼可能是可能的原因是什麼?

類似的問題(!不是由我)以前提出的,但對這個問題沒有人回答說:Application not running in iOS 5


據我瞭解到目前爲止,只有EXE_BAD_ACCESS當您試圖訪問還沒有內存地址發生已被分配,或先前已分配但現在已被釋放。

修改回覆IN後評論:

嘿Firoze,這是怎麼了初始化NSURLConnection的

urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
+0

@FirozeLafeer嘿夥計!我已經添加了init方法。有問題:)謝謝。我在那裏發佈NSString,因爲我是alloc並且init是相同的函數。我想我應該釋放它是嗎?否則會泄漏 – doNotCheckMyBlog

+0

responseData來自哪裏?無論它在哪裏創建,它都被保留下來了嗎? – coneybeare

+0

@Brogrammer無視我對responseString的評論。沒關係,我沒有直接在那裏想。我錯過了那部分。 –

回答

3

在您的意見看,我建議你使用@property聲明對所有高德的。他們將減輕你所要做的所有手動內存管理,這可能是你的問題所在。

簡單的例子

YourClass.h

@interface YourClass 
@property (nonatomic, retain) NSURLConnection *urlConnection; 
// More ivars 

// Method declations 
@end 

YourClass.m

@interface YourClass 

@synthesize urlConnection = _urlConnection; 

// The method where you instantiate urlConnection 
{ 
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request 
                    delegate:self]; 
    self.urlConnection = urlConnection; 
    [urlConnection release]; urlConnection = nil; 

    // Do whatever else you do here 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [self.urlConnection cancel]; 
    self.urlConnection = nil;  <- This takes care of releasing the ivar and setting it to nil so there is no dangerous hanging pointer 

    // ... the rest of your method 
} 

[urlConnection cancel]; 
[urlConnection release]; 
// You need to clean up after yourself with any ivars you make 
- (void)dealloc 
{ 
    [_urlConnection release]; 
    // Release any other ivars 
    [super dealloc]; 
} 
@end 
+0

我剛剛檢查了我的dealloc和viewDidUnload從來沒有被het消息過?這是不尋常的嗎?似乎我dealloc中的所有一系列發佈方法都是浪費:( – doNotCheckMyBlog