我構建了一個簡單的應用程序,它從HockeyApp獲取報告。但是,當我使用內存泄漏工具運行應用程序時,它顯示執行getReport操作時發生內存泄漏。我無法理解儀器中顯示的所有信息。如何修復Objective-C中的內存泄漏?
這裏是這會導致內存泄漏按鈕動作方法:
- (IBAction)getReports:(id)sender {
//initialize url that is going to be fetched.
NSURL *url = [NSURL URLWithString:@"https://rink.hockeyapp.net/api/2/apps/APP_ID/crash_reasons"];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:tokenReceived forHTTPHeaderField:@"X-HockeyAppToken"];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.getReportConnection = connection;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data{
if (connection==getReportConnection) {
[self.receivedData appendData:data];
NSLog(@"data is %@",data);
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSError *e = nil;
NSData *jsonData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData options: NSJSONReadingMutableContainers error: &e];
NSLog(@"login json is %@",JSON);
NSLog(@"reason json is %@",JSON[@"reason"]);
[JSON[@"crash_reasons"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[reportArray addObject:obj[@"reason"]];
NSLog(@"index = %lu, Object For title Key = %@", (unsigned long)idx, obj[@"reason"]);
}];
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData
options:kNilOptions error:&error];
if (error != nil) {
NSLog(@"Error parsing JSON.");
}
else {
NSLog(@"Array: %@,array count is %d", jsonArray,jsonArray.count);
}
// [reportArray addObject:[jsonArray objectAtIndex:0]];
if (JSON!=NULL) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Reports succesfully retrieved" message:@"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
}
}
// This method receives the error report in case of connection is not made to server.
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *errorAlert=[[UIAlertView alloc]initWithTitle:@"Wrong Login" message:nil delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[errorAlert show];
NSLog(@"error is %@",error);
}
// This method is used to process the data after connection has made successfully.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
}
我看到之前的警報視圖顯示在didRecieveData
方法發生內存泄漏。
這裏是表示內存泄漏內存泄漏儀器的屏幕截圖:
我無法理解其部分代碼導致內存泄漏。任何人都可以告訴我如何識別導致泄漏儀器泄漏內存的代碼部分?
編輯:當運行在模擬器上的應用程序,該儀器沒有顯示任何內存泄漏:
這裏是截圖:
再次當我在設備運行應用程序,該儀器向我展示了內存泄漏:
我看着泄漏部分,我發現NSmutableArray
是造成泄漏:
我在我的代碼中只使用了一個NSMutableArray
。我宣佈它在.h
文件:
@property (nonatomic,strong) NSMutableArray *reportArray;
和viewDidLoad
分配它:
reportArray=[[NSMutableArray alloc]init];
和didRecieveData
裝好了:
[reportArray addObject:obj[@"reason"]];
堆棧跟蹤快照:
請注意,右側「快照」之上的一個工具按鈕選擇一個堆棧跟蹤,可以顯示泄漏對象的分配情況。 –
添加了堆棧跟蹤快照! –