Xcode is telling me that there are some problems with the code below in terms of memory leaking.
@property (nonatomic, retain) NSMutableArray *naw_rows;
-(void) loadTableRows:(BOOL)shouldReload
{
[naw_rows removeAllObjects];
[self.naw_rows addObject: [[CellModel alloc] initialize:@"name" title:@"Name" value: self.currentProfile.name]];
[self.naw_rows addObject: [[CellModel alloc] initialize:@"company" title:@"Company name" value: self.currentProfile.company]];
[self.naw_rows addObject: [[CellModel alloc] initialize:@"address" title:@"Address" value: self.currentProfile.address]];
[self.naw_rows addObject: [[CellModel alloc] initialize:@"zipcode" title:@"Zipcode" value: self.currentProfile.zipcode]];
[self.naw_rows addObject: [[CellModel alloc] initialize:@"city" title:@"City" value: self.currentProfile.city]];
}
// here is my cellModel object:
@implementation CellModel
-(id) initialize:(NSString *)newName title:(NSString *)newTitle value:(NSString *)newValue;
{
if (self == [super init])
{
name = newName;
title = newTitle;
value = newValue;
}
return self;
}
- (NSString *) getName
{
return name;
}
- (NSString *) getTitle
{
return title;
}
- (NSString *) getValue
{
return value;
}
-(void)dealloc
{
[super dealloc];
}
@end;
All the addObject lines give the following error:
Potential leak of an object allocated on line — Method returns an
Objective-C object with a +1 retain count (owning reference) Object
allocated on line — is not referenced later in this execution path
and has a retain count of +1 (object leaked)
In other topics about memory leaking i found that this would be the correct way to do this:
CellModel *model = [[CellModel alloc] initialize:@"name" title:@"Name" value: self.currentProfile.name];
[self.naw_rows addObject: model];
[model release];
But this gives me the following error:
Incorrect decrement of the reference count of an object that is not
owned at this point by the caller
So what am i doing wrong? In my first piece of code the retain count should be 1. Owned by the array. And i assume the objects are released when i use [array remodeAllObjects]
Thanks in advance,
Nico
This leaks:
You own the object returned by alloc-init and you are responsible for relinquish ownership of it by sending it a
releaseorautoreleasemessage, which you fail to do.Using a temporary variable, as you propose, does solve the problem:
So, why does the analyzer complain? Because of the name of your initializer. Rename it to something like
initWithName:title:value:and you’ll see the “Incorrect decrement of the reference count of an object that is not owned at this point by the caller” go away.The convention is that the name of initializer methods should begin with the abbreviation
init.Also, the implementation of your class does not assign self to the result of calling super’s initializer. This:
should be:
or, if you prefer:
Also, the memory management of the instance variables of the
CellModelclass is wrong. You should retain or copy the objects passed as arguments to your init method and also release them in dealloc.The accessor methods also break the naming conventions, they should be named simply
name,title, etc. The prefix “get” is only for methods that return objects indirectly.