views:

86

answers:

2

Hi all,

I am writing an iPad app that downloads a rather large .csv file and parses the file into objects stored in Core Data. The program keeps crashing, and I've run it along with the Allocations performance tool and can see that it's eating up memory.

Nothing is alloc'ed or init'ed in the code, so why am I gobbling up memory?

Code at: http://pastie.org/955960

Thanks! -Neal

+1  A: 

Does the memory footprint shrink after the loop finishes? Objects created and autoreleased will stick around during the while loop. You may need to maintain your own autorelease pool using NSAutoreleasePool.

MrHen
I'd go with the autorelease pool theory, too.
Paul Lynch
A: 

There's no leaks. But you're allocating a new array and string every loop, which will accumulate when the input is very long, before the autorelease pool can drain.

while ( ![scanner isAtEnd] ) {
    BOOL insideQuotes = NO;
    BOOL finishedRow = NO;
    NSMutableArray *columns = [NSMutableArray arrayWithCapacity:10];
    NSMutableString *currentColumn = [NSMutableString string];

Since these are temporary variables, you could just move them outside of the loop, and reset them on an iteration.

NSMutableArray *columns = [NSMutableArray arrayWithCapacity:10];
NSMutableString *currentColumn = [NSMutableString string];
while ( ![scanner isAtEnd] ) {
    BOOL insideQuotes = NO;
    BOOL finishedRow = NO;
    [columns removeAllObjects];
    [currentColumn setString:@""];
KennyTM