tags:

views:

170

answers:

2

Instruments tells me the following line from the code below is leaking: I can't figure out how to fix this leak.

[self.selectedElement.usrAdvancedBuyingPercents replaceObjectAtIndex:selectedRow withObject:[numberFormatter stringFromNumber:percentage]];

 - (IBAction) simpleMarginSliderValueChanged:(UISlider *)sender {  

NSDecimalNumber *percentage = (NSDecimalNumber *)[NSDecimalNumber numberWithFloat:[sender value]];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:@"#.##"];

[self.selectedElement.usrAdvancedBuyingPercents replaceObjectAtIndex:selectedRow withObject:[numberFormatter stringFromNumber:percentage]];

[numberFormatter release];


}
A: 

The code you posted looks correct to me. Can you post more specific information? (ie, what does Instruments say you're leaking?)

Dave DeLong
It says i'm leaking the string generated from the numberFormatter
radesix
@radesix I'm guessing something weird is going on with your `self.selectedElement.usrAdvancedBuyingPercents` call, because the static analyzer says there's nothing wrong w/ your code.
Dave DeLong
That being said, usrAdvancedBuyingPercents is an array of strings that are read in from a plist. When they are brought in from the plist it's simply an array to array copy. Do I need to be bringing the strings in individually using an NSString object?
radesix
+1  A: 

The NSString you are creating from the number is not being released somewhere.

The problem is not in the code that is shown - it's somewhere else that is taking a string from that array, retaining it, then not releasing it. Leaks just shows you where memory that is leaked was initially allocated, and the only thing on that line that is allocating memory is [numberFormatter stringFromNumber:percentage].

Either that, or the whole array is not being released correctly (but then whatever builds usrAdvancedBuyingPercents would also show that is leaking).

Kendall Helmstetter Gelner