0
假设我有这样的iPhone - 如何声明的方法和避免泄密
- (UIButton *) createButtonAtX:(CGFloat)vX Y:(CGFloat)vY{
//... bla bla bla...
//at some point I have
UIButton *myButton = [[UIButton alloc] initWithFrame:myFrame];
// we have an alloc here...
// ... more bla bla and then
return myButton;
}
作为按钮的方法被分配并没有公布这在技术上是有泄漏,对不对?
在我的主要代码,主叫方就会像
UIButton *oneButton = [self createButtonAtX:100 Y:100];
[myView addSubview:oneButton];
[oneButton release];
从理论上讲,oneButton就是为myButton上createButton方法上主要的代码被释放,但即便如此,仪器将指向方法泄漏...
如何解决?使用autorelease?
感谢
Apple的内存管理编程指南很好地解释了这里发生了什么。 http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW5 – 2010-10-07 05:04:30
但我怎么做,当我使用像CGImageMaskCreate这样的石英函数,需要使用CGImageRelease来发布。我不能简单地把它作为autorelease,或者我可以吗?假设我使用类似于...... CGImageRef mask = CGImageMaskCreate(...并在最后有一个返回掩码;如果我需要返回CGImageRef,我该怎么办? – SpaceDog 2010-10-07 05:09:21
实际上,您可以为任何Core Foundation对象。在你的例子中,你会返回[(id)mask autorelease]。 – Costique 2010-10-07 05:22:48