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