Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS memory leak according to instruments
    primarykey
    data
    text
    <p><img src="https://i.stack.imgur.com/dEkoF.png" alt="Screen shot of memory leak in instruments"></p> <p>Please help! I've read the memory management rules, but maybe I'm missing them point somewhere. Instruments is telling me I've got leaks on the following code:</p> <pre><code>NSArray *keys = [NSArray arrayWithObjects:@"text", @"score", @"subCount", nil]; NSArray *objects = [NSArray arrayWithObjects:sPlateToAdd, [ [[NSNumber alloc] initWithInt:1] autorelease], [[[NSNumber alloc] initWithInt:1] autorelease], nil]; NSMutableDictionary *dPlateToAdd = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys]; // 93.4% [self.aFinals addObject:dPlateToAdd]; // 6.6% </code></pre> <p>the Keys and Objects arrays aren't being alloc'ed or init'ed, so I dont think I need to release those?</p> <p>Then the numbers inside Objects are being auto released, so they're ok aren't they? And sPlateToAdd is a string that gets passed into the method this code is in, so I'm not the owner of that, so I don't need to release it. Or am I?</p> <p>I've got to be doing something wrong somewhere.</p> <p>The app runs completely fine in the iPad, but is dog slow on an iPhone 3GS, I'm hoping fixing this memory leak might speed it up a little...</p> <p>This is the method that creates self.aFinals, which is passed a string from a text input. I've ommitted some of the lines but self.aFinals doesn't interact with them</p> <pre><code>-(id)initWithTerm:(NSString *)thisTerm { ... ... self.aFinals = [[NSMutableArray alloc] init]; return self; } </code></pre> <p>Then I have about 5 nested loops, that call addPlateToFinals 3 times in the middle of all the loops, creating <strong>thisPlate</strong>, which becomes <strong>sPlateToAdd</strong></p> <pre><code>// replace 1st occurance NSString *thisPlate = [thisBase stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat: @"(^[^%@]+)%@(.*$)", thisChar, thisChar] withString:[NSString stringWithFormat:@"$1%@$2", thisSub] ]; [self addPlateToFinals:thisPlate withSubCount:thisSubCount]; // replace 2nd occurance thisPlate = [thisBase stringByReplacingOccurrencesOfRegex:[NSString stringWithFormat: @"(^[^%@]+%@.*)%@", thisChar, thisChar, thisChar] withString:[NSString stringWithFormat:@"$1", thisSub] ]; // then it does it again, with slightly different regex </code></pre> <p>This is the complete method that the leak is coming from:</p> <pre><code>-(void)addPlateToFinals:(NSString *)sPlateToAdd withSubCount:(NSNumber *)nSubCount { // plate must be less than 7 characters and great than 2 chars if ( [sPlateToAdd length] &lt;= [self.nPlateMax intValue] &amp;&amp; [sPlateToAdd length] &gt;= [self.nPlateMin intValue] ) { NSMutableArray *aSearchFinals = [self arrayOfFinals]; // add plate if it is not already in the finals array if(![aSearchFinals containsObject:sPlateToAdd]) { // filter out results that cannot be converted to valid plates NSPredicate *potential = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z]{0,3}[0-9]{1,3}[a-z]{0,3}$'"]; NSPredicate *impossible1 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z]{2}[0-9]{2,3}[a-z]{2}$'"]; NSPredicate *impossible2 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z][0-9]{3}$'"]; NSPredicate *impossible3 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[a-z]{2}[0-9]{2}$'"]; NSPredicate *impossible4 = [NSPredicate predicateWithFormat: @"SELF MATCHES '^[0-9]{2}[a-z]{2}$'"]; if( [potential evaluateWithObject: sPlateToAdd] &amp;&amp; ![impossible1 evaluateWithObject: sPlateToAdd] &amp;&amp; ![impossible2 evaluateWithObject: sPlateToAdd] &amp;&amp; ![impossible3 evaluateWithObject: sPlateToAdd] &amp;&amp; ![impossible4 evaluateWithObject: sPlateToAdd] ){ NSArray *keys = [NSArray arrayWithObjects:@"text", @"score", @"subCount", nil]; NSArray *objects = [NSArray arrayWithObjects: sPlateToAdd, [[[NSNumber alloc] initWithInt:1] autorelease], [[[NSNumber alloc] initWithInt:1] autorelease], nil ]; NSDictionary *dPlateToAdd = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; [self.aFinals addObject:dPlateToAdd]; } } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload