The following method shows up as leaking while performing a memory-leaks test with Instruments:
- (NSDictionary*) initSigTrkLstWithNiv:(int)pm_SigTrkNiv SigTrkSig:(int)pm_SigTrkSig SigResIdt:(int)pm_SigResIdt SigResVal:(int)pm_SigResVal {
NSArray *objectArray;
NSArray *keyArray;
if (self = [super init]) {
self.SigTrkNiv = [NSNumber numberWithInt:pm_SigTrkNiv];
self.SigTrkSig = [NSNumber numberWithInt:pm_SigTrkSig];
self.SigResIdt = [NSNumber numberWithInt:pm_SigResIdt];
self.SigResVal = [NSNumber numberWithInt:pm_SigResVal];
objectArray = [NSArray arrayWithObjects:SigTrkNiv,SigTrkSig,SigResIdt,SigResVal, nil];
keyArray = [NSArray arrayWithObjects:@"SigTrkNiv", @"SigTrkSig", @"SigResIdt", @"SigResVal", nil];
self = [NSDictionary dictionaryWithObjects:objectArray forKeys:keyArray];
}
return self;
}
code that invokes the instance:
NSDictionary *lv_SigTrkLst = [[SigTrkLst alloc]initSigTrkLstWithNiv:[[tempDict objectForKey:@"SigTrkNiv"] intValue]
SigTrkSig:[[tempDict objectForKey:@"SigTrkSig"] intValue]
SigResIdt:[[tempDict objectForKey:@"SigResIdt"] intValue]
SigResVal:[[tempDict objectForKey:@"SigResVal"] intValue]];
[[QBDataContainer sharedDataContainer].SigTrkLstArray addObject:lv_SigTrkLst];
[lv_SigTrkLst release];
Instruments informs that ‘SigTrkLst’ is leaking. Even though I have released the instance?
(I know that adding it to the array increments the retainCount by 1 but releasing it twice removes it from the array?)
You are initializing the instance (
self), but then replacing it with a dictionary. Assuming that is what you actually want to do (see part 2), you need to release the oldselfbefore assigning a new one, and then you must retain the new dictionary so that yourinitmethod preserves the retain count. Note that this code returns aNSDictionaryobject, when your calling code expects aSigTrkLst, which is almost certainly a bad idea.Part 2: What is this supposed to do? Normally speaking, an
-init...method should return an initialized class instance, starting from an allocated chunk of memory. The first part of your-initmethod looks right, other than the return value. For example, the following is a normal-initmethod: