why do I need this autorelease after [NSMutableArray array] to avoid a memory leak?
That is Instruments told me there was a leak. By putting the autorelease in it solved it, however I’m not sure why this would be required. The “array” method wasn’t like an INIT or COPY etc…
@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
NOTE: This is what I see in Intruments when I take autorelease out (and after I changed the “if (self == [super init])” to “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);")
Why do you need that extra release? You don’t. Not there, anyway.
The problem is you’re overretaining
_eventssomewhere else. Maybe you’re passing it to another class that’s retaining without releasing? Leaks are always attributed by Instruments to creation of the object, not the unbalanced retain.Adding that autorelease instead of finding the unbalanced retain is the memory management equivalent of having an answer that’s off by 0.3 and just adding 0.3 to fix it. You need to remove that and fix the real problem.
Edit: After reading your latest edit, I think you’ll probably find that
Weekenditself is being leaked.