Re: Analyser reports memory leak… where?
Re: Analyser reports memory leak… where?
- Subject: Re: Analyser reports memory leak… where?
- From: Aaron Montgomery <email@hidden>
- Date: Thu, 12 Sep 2013 09:08:19 -0700
Right, sorry, threw it together too quickly and have been living too long in the land of ARC. Correcting the setter, fixing the dealloc method and removing the "newObject" method (see below):
@interface MWObject : NSObject
@property (nonatomic, strong) NSObject* object;
@end
@implementation MWObject
@synthesize object=_object;
- (void)dealloc
{
[_object release];
[super dealloc];
}
- (void)setObject:(NSObject *)object
{
if (object == _object) return;//5
[object retain];
[_object release];
_object = object;//3
}
- (NSObject*)object
{
NSObject* theObject = [[NSObject alloc] init];
[self setObject:theObject];//2
[theObject release];//1
return _object;//4
return [[_object retain] autorelease];
}
@end
Version 0: Comment out line 5, as written has no warnings
Version 1: Comment out line 1 and 5, has no warnings
Version 2: Comment out lines 1 and 2 and 5, has a warning
Version 3: Comment out lines 1 and 3 and 5, has a warning.
Again, Versions 0 and 1 cannot both have correct memory management.
Note that if we comment out line 4, we have:
Version 4: Comment out line 4 and 5, no warnings
Version 5: Comment out lines 4 and 1 and 5, no warnings.
So not returning an retained-autoreleased value from the getter, is not the issue.
However, if we add a branch test for preventing self-assignment, we do get a warning when expected:
Version 6: As written above, no warnings
Version 7: Comment out line 1, warning
Also note that the naming of the newObject method is not the core issue here. We can eliminate that method entirely and we will still get the odd behavior.
Aaron
On Sep 12, 2013, at 8:28 AM, "Gary L. Wade" <email@hidden> wrote:
> In your dealloc, you should release ivars before calling super dealloc. Ideally a crash will tell you that quickly; otherwise things could be very bad.
> --
> Gary L. Wade (Sent from my iPhone)
> http://www.garywade.com/
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden