问题
Anyone able to help re what is the source of the leaky object for this code:
My application compiles without any ANALYZE issues. When I run PROFILER and look at Memory Leaks I see leaks appears. One of these leaks is in my WeekendEvent object. There are 3 Leaked Block row items in instruments per the below (I've noted in the code where these point to):
- Malloc +1
- Retain +2
- Release +1
Question - I assume this implies there is a release, however where would this leak be from. The segments of my code that clicking through on instruments highlights is below. To me it seems OK in that:
- the WeekendEvent I create I release after passing into the addWeekendEvent method
- in the addWeekendEvent it just adds it to a NSMutableArray, and hence I thought the arrange does any memory management for it's object it contains
- I do release the NSMutableArray in the dealloc too
Key Source Code Bits & What Instuments Highlights
// ------Weekends Method (plural)-----
WeekendEvent *weEvent = [[WeekendEvent alloc] init]; // [INSTRUMENTS - 87.5%]
[week addWeekendEvent:weEvent]; // [INSTRUMENTS - 12.5%]
[weEvent release];
//------Weekend *.h ------------
@interface Weekend : NSObject {
NSMutableArray* _events;
}
- (void)addWeekendEvent:(WeekendEvent*)weEvent;
@property (nonatomic, retain) NSMutableArray* events;
@end
//------Weekend *.m -------------
- (void)addWeekendEvent:(WeekendEvent*)weEvent {
[self.events addObject:weEvent];
}
- (void) dealloc {
[_events release];
[super dealloc];
}
EDIT: Some additional code re how the "week" variable above was created/used - so in the Weekends Method the code I posted was within a for loop - the code with the for loop shown therefore was:
for (Weekend *week in self.items) {
// do pass "week.start" to some methods (which is an NSDate) - don't think this would be relevant though?
WeekendEvent *weEvent = [[WeekendEvent alloc] init]; // [INSTRUMENTS - 87.5%]
[week addWeekendEvent:weEvent]; // [INSTRUMENTS - 12.5%]
[weEvent release];
}
// Note - self.items I checked is "released" in the dealloc method
EDIT 2 - Just to confirm, it is an "WeekendEvent" instance that Instruments highlights in it's "leaked objects" column. Just in case this wasn't clear.
EDIT 3 - Re how I setup the items variable - key code bits are:
@interface Weekends : NSObject {
NSMutableArray* _items;
}
@property (nonatomic, retain) NSMutableArray* items;
@synthesize items = _items;
- (void) dealloc {
[_items release];
[super dealloc];
}
回答1:
The memory management in the code you show is correct, assuming that the rest of your Weekend
class looks something like this:
@synthesize events = _events;
- (id)init {
if ((self = [super init]) == nil) { return nil; }
_events = [[NSMutableArray alloc] initWithCapacity:0];
return self;
}
Also, the instruments results match all of your code:
Malloc +1 == WeekendEvent *weEvent = [[WeekendEvent alloc] init];
Retain +2 == [week addWeekendEvent:weEvent];
Release +1 == [weEvent release];
Based on that logic, the most likely candidate is that your week
object is not properly released. You haven't shown the code that explains how it was created, but I do notice that the code you did post is for a Weekend
class. Are you sure week
is not of a different type?
来源:https://stackoverflow.com/questions/6119023/what-is-the-source-of-the-leaky-object-for-this-code