user542584
user542584

Reputation: 2699

using custom init method in iphone -identifying a leak

For one of my custom classes, I have defined a method called initialize to set some instance variables at the same time as the init. The code is below. The analyzer tool is reporting a leak in viewDidLoad on the line with [[Employee alloc].. Since I am releasing the variable in the dealloc, I thought that this should be fine..what could be the issue? thanks in advance.

@interface testViewController : UIViewController <UITextFieldDelegate>{
    Employee *employee;
}
- (void)viewDidLoad {  
if(employee ==nil) 
   employee = [[Employee alloc] initialize:@"John"];    

if (![employee.entityName isEqualToString:@"Test"]) { //The leak is reported here for object allocated above
    ///...
}

}

- (void)viewDidUnload {
    [super viewDidUnload];
    employee = nil;
}

- (void)dealloc {
    [super dealloc];
  [employee release];

}

//In the Employee class
-(id) initialize:(NSString*) name{
    self = [super init];

    self.entityName = name;


    return self;
}

Upvotes: 0

Views: 283

Answers (1)

Tom Jefferys
Tom Jefferys

Reputation: 13310

In your viewDidUnLoad you need to release employee before it gets set to nil. Else in your dealloc, you are just releasing nil.

ie

- (void)viewDidUnload {
    [super viewDidUnload];
    [employee release];
    employee = nil;
}

Upvotes: 2

Related Questions