目标c,实例成员的内存管理 [英] Objective c, Memory management of instance members

查看:90
本文介绍了目标c,实例成员的内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对实例成员的内存管理感到困惑。我有一个带有ivar的课程:

I am confuzed by the memory management of instance members. I have a class with a ivar:

DetailedResultsTableViewController *detailedResultsTableViewController;

@property (nonatomic, retain) DetailedResultsTableViewController *detailedResultsTableViewController;

@synthesize detailedResultsTableViewController;

[detailedResultsTableViewController release];

当我初始化这个变量时:

When I initial this variable:

self.detailedResultsMapViewController = [[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil];

我在初始化后测试了retaincount,它是2!如果我在函数结束时释放它,它将落入未分配的对象。我究竟做错了什么?我应该如何初始化这种类型的变量?
谢谢!

I tested the retaincount right after this init and it is 2 !!! if i release it in the end of the function it will fall for not allocated object. What am I doing wrong? how should I initial this type of variable? Thanks!!

推荐答案

首先你不应该看看零售额,它不是真的可靠。

First you should not look at the retaincount, it not really reliable.

第二个属性设置为保留。因此,当您为其分配一些东西时,它会增加重新计算。和分配一样。

Second your property is set to retain. So when you assign some thing to it, it will increase the reatincount. As will alloc.

这样做就是泄漏:

self.detailedResultsMapViewController = [[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil];

你应该这样做:

DetailedResultsMapViewController *vc = [[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil];
self.detailedResultsMapViewController =vc;
[vc release], vc= nil;

或使用Autorelease:

Or use Autorelease:

self.detailedResultsMapViewController = [[[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil] autorelease];

这篇关于目标c,实例成员的内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆