开发者

Help Finding Memory Leak

开发者 https://www.devze.com 2022-12-30 03:28 出处:网络
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 performanc

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:@""];
0

精彩评论

暂无评论...
验证码 换一张
取 消