Gre*_*reg 1 iphone memory-management autorelease nsmutablearray ios
为什么在[NSMutableArray数组]后需要这个自动释放以避免内存泄漏?
那是仪器告诉我有泄漏.通过将自动释放器解决它,但是我不确定为什么要这样做."数组"方法不像INIT或COPY等......
@interface Weekend : NSObject {
NSMutableArray* _events;
}
@property (nonatomic, retain) NSMutableArray* events;
@end
@implementation Weekend
@synthesize events = _events;
- (id)init {
if (self == [super init])
{
self.events = [[NSMutableArray array] autorelease]; // WHY IS THIS AUTORELEASE REQUIRED
}
return self;
}
- (void) dealloc {
[_events release]; _events = nil;
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
注意:这是我在Intruments中看到的,当我取出autorelease out(并且在我将"if(self == [super init])"改为"if((self = [super init]))"之后
# Category Event Code Location
0 __NSArrayM Malloc at the [NSMutableArray array] point
1 __NSArrayM Autorelease at the [NSMutableArray array] point
2 __NSArrayM Retain at the @synthesize events = _events; point of the code
3 __NSArrayM Release QuartzCore - CA:Transaction::observer_callback(__CF........)
(from main.m:14 - "int retVal = UIApplicationMain(argc, argv, nil, nil);")
Run Code Online (Sandbox Code Playgroud)
为什么需要额外的版本?你没有.反正不在那里.
问题是你在其他_events地方已经过度了.也许你把它传递到另一个没有释放的班级?仪器总是将泄漏归因于物体的创造,而不是不平衡的保留.
添加自动释放而不是找到不平衡的保留是内存管理相当于有一个关闭0.3的答案,只需添加0.3来修复它.你需要删除它并修复真正的问题.
编辑:阅读完最新的编辑后,我想你可能会发现Weekend自己被泄露了.
| 归档时间: |
|
| 查看次数: |
2893 次 |
| 最近记录: |