Memory efficient collection class

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2010-05-28T16:46:28Z Indexed on 2010/05/29 8:22 UTC
Read the original article Hit count: 229

Filed under:
|

I'm building an array of dictionaries (called keys) in my iphone application to hold the section names and row counts for a tableview.

the code looks like this:

[self.results removeAllObjects];
[self.keys removeAllObjects];

NSUInteger i,j = 0;
NSString *key = [NSString string];
NSString *prevKey = [NSString string];

if ([self.allResults count] > 0)
{
    prevKey = [NSString stringWithString:[[[self.allResults objectAtIndex:0] valueForKey:@"name"] substringToIndex:1]];

    for (NSDictionary *theDict in self.allResults)
    {
        key = [NSString stringWithString:[[theDict valueForKey:@"name"] substringToIndex:1]];

        if (![key isEqualToString:prevKey])
        {
            NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithInt:i],@"count",
                                           prevKey,@"section",
                                           [NSNumber numberWithInt:j],
                                           @"total",nil];

            [self.keys addObject:newDictionary];
            prevKey = [NSString stringWithString:key];
            i = 1;
        }
        else
        {
            i++;
        }
        j++;

    }

    NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:i],@"count",
                                   prevKey,@"section",
                                   [NSNumber numberWithInt:j],
                                   @"total",nil];

    [self.keys addObject:newDictionary];

}

[self.tableview reloadData];

The code works fine first time through but I sometimes have to rebuild the entire table so I redo this code which orks fine on the simulator, but on my device the program bombs when I execute the reloadData line :

malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Program received signal:  “EXC_BAD_ACCESS”.

If I remove the reloadData line the code works on the device.

I'm wondering if this is something to do with the way I've built the keys array (ie using autoreleased strings and dictionaries).

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c