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
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.
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:@""];
精彩评论