【发布时间】:2011-08-13 05:53:01
【问题描述】:
我的 UIViewController 中有一个 UIScrollView,在我的 .h 文件中定义为:
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController <UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView * imageScrollView;
@end
然后在我的 .m 文件中,我有以下内容:
@synthesize imageScrollView = _imageScrollView;
我读到这将自动创建我通常会在 .h 文件中键入的 _imageScrollView? (UIScrollView * _imageScrollView)
我喜欢它,因为它从我的 .h 文件中删除了重复的代码。现在,在我的 loadView 中,我完成了剩下的工作:
self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0 - 20.0 - 49.0)];
[_imageScrollView setDelegate:self];
[_imageScrollView setPagingEnabled:YES];
[_imageScrollView setBounces:NO];
[_imageScrollView setShowsHorizontalScrollIndicator:NO];
[_imageScrollView setShowsVerticalScrollIndicator:NO];
[_imageScrollView setContentSize:CGSizeMake(320.0 * 3.0, 480.0 - 20.0 - 49.0)];
在 dealloc 版本中和 nil:
- (void)dealloc
{
[_imageScrollView release], _imageScrollView = nil;
[super dealloc];
}
现在构建后 Xcode 告诉我这个:
Potential leak of an object allocated on line #linenumber
当我改变它时它会消失:
self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0 - 20.0 - 49.0)];
到这里:
self.imageScrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0 - 20.0 - 49.0)] autorelease];
为什么我在 dealloc 中释放它时需要自动释放它?我做错了什么?
此内存警告仅出现在安装了 Lion 的 iMac 上的 Xcode 中,而不出现在安装有雪豹的 Macbook 上...
【问题讨论】:
标签: objective-c xcode properties release