Ste*_*aly 0 iphone memory-management objective-c autorelease ios
好的,所以我知道在这个问题上有一堆问题,但在阅读它们并尝试使用这些方法之后,我的应用程序似乎仍然会泄漏内存.我研究上的内存Manegment苹果直营店和读取显着的问题在这里,这里和这里.我有一个方法解析一个JSON字符串,然后将它们返回到一个NSMutableDictionary.我autorelease从返回方法调用该对象,然后调用retain接收方法(确保该对象在池的下一个排水管上不是dealloc).然后,当我完成它时,我释放对象.但它仍然泄漏.谁能看到我做错了什么?
退货方法
+ (NSMutableArray *)parseSpecialtyResult:(NSString *)json
{
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dictionary = [parser objectWithString:json];
NSArray *jsonObjects = [dictionary valueForKey:@"Rows"];
NSMutableArray *jsonArray = [[NSMutableArray alloc] initWithCapacity:[jsonObjects count]];
//Storing objects
for (NSDictionary *dict in jsonObjects)
{
Specialty *specialty = [[Specialty alloc] init];
[specialty setName:[dict objectForKey:@"name"]];
[specialty setIdenity:[dict objectForKey:@"id"]];
[jsonArray addObject:specialty];
}
[parser release];
//Relinquish ownership of this object
return [jsonArray autorelease];
}
Run Code Online (Sandbox Code Playgroud)
打电话给班级
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
//This class will take the responsibility of the object's ownership
self.jsonArray = [[SpecialtyUrl parseSpecialtyResult:responseString] retain];
[tableView reloadData];
[responseString release];
}
- (void)dealloc
{
[super dealloc];
//No longer need the object
[jsonArray release];
NSLog(@"Ref count %i", [jsonArray retainCount]);
}
Run Code Online (Sandbox Code Playgroud)
日志
Ref count 1
Run Code Online (Sandbox Code Playgroud)
您不会Speciality在for语句中释放对象:
for (NSDictionary *dict in jsonObjects)
{
Specialty *specialty = [[Specialty alloc] init];
...
[jsonArray addObject:specialty];
[specialty release];
}
Run Code Online (Sandbox Code Playgroud)
此外,如果jsonArray是保留或复制属性,则会过度保留对象:
self.jsonArray = [[SpecialtyUrl parseSpecialtyResult:responseString] retain];
Run Code Online (Sandbox Code Playgroud)
应该简单(再次,如果保留或复制):
self.jsonArray = [SpecialtyUrl parseSpecialtyResult:responseString];
Run Code Online (Sandbox Code Playgroud)
此外,[super dealloc];应该是最后一个声明dealloc.
| 归档时间: |
|
| 查看次数: |
353 次 |
| 最近记录: |