【发布时间】:2011-04-10 04:17:30
【问题描述】:
你好 Stackoverflow 的家人!
我有一个关于 iPhone 内存管理的问题。
我所理解的是下面的方法
-(void) dealloc
{
// something else to release whatever
// such as Object Created using keyword 'alloc'
// but also Object destroy here its retain value reaches iff 0
// if I do put here NSLog(@"%d", [obj retainCount]); and when it reaches
// not equal to 0 means failure with memory leak.
[super dealloc];
}
所以我理解对了吗?还是这里即使retain count达到> 0仍然亮着?
我问这个问题的原因是,
我检查过
NSLog(@"%d", obj.retainCount);
检查对象的保留计数并收到值3。所以我尝试在这里释放3次以使retainCount这里等于0,但是编译器给了我严重错误。
拜托,我是内存释放和保留、释放的新手。
我使用的对象是“UIImageView”对象并创建了另一个实例,
UIImageView *imageView = //da da~ with UIImage
UIImageView *instance;
// at this point retain count was '1'
instance = imageView;
//[imageView retain];
// at this point retain count was '2'
[self.view addSubView: imageView];
// at this point retain count was '3'
[imageView release];// crashes
// at this point retain count was '2'
如果我这样做了
// but if I add retain on the 'instance = imageView'
// such as
instance = imageView; // then
[imageView retain];
// works but still count is 2...
谢谢。
【问题讨论】:
标签: iphone objective-c memory-management