我正在从文本文件中将一堆数据加载到CoreData中。出于某种原因,在下面的代码中通过for(行中的行)循环的每次迭代都是分配新的内存。它工作正常,直到它无法分配新的内存。我不明白为什么它不会为每次迭代重复使用相同的内存。我究竟做错了什么?任何帮助将不胜感激。
`
- (IBAction)loadDataInThread {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MileMarkers" ofType:@"csv"];
NSData *myString = [NSString stringWithContentsOfFile:filePath];
NSArray *lines = [myString componentsSeparatedByString:@"\r"];
status.hidden = FALSE;
// NSManagedObjectContext *context = [self managedObjectContext];
NSFileManager *fileman = [NSFileManager defaultManager];
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Tiles"];
NSString *line;
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]];
[myNumFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSManagedObject *newManagedObject;
NSArray *fields;
// NSLog(@"Loading Markers");
for (line in lines) {
fields = [line componentsSeparatedByString:@","];
self.stat = [NSString stringWithFormat:@"%@ %@ %@ %@",[fields objectAtIndex:0],[fields objectAtIndex:1],[fields objectAtIndex:2],[fields objectAtIndex:3]];
NSLog(stat);
[self performSelectorOnMainThread:@selector(updateStatus) withObject:nil waitUntilDone:YES];
newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"MileMarkers" inManagedObjectContext:self.managedObjectContext];
NSNumber *f1 = [myNumFormatter numberFromString:[fields objectAtIndex:0]];
NSNumber *f2 = [myNumFormatter numberFromString:[fields objectAtIndex:1]];
NSNumber *f3 = [myNumFormatter numberFromString:[fields objectAtIndex:2]];
NSNumber *f4 = [myNumFormatter numberFromString:[fields objectAtIndex:3]];
[newManagedObject setValue:f1 forKey:@"ICWID"];
[newManagedObject setValue:f2 forKey:@"MileMarker"];
[newManagedObject setValue:f3 forKey:@"Latitude"];
[newManagedObject setValue:f4 forKey:@"Longitude"];
NSMutableArray *charts = [[NSMutableArray alloc] initWithCapacity:4];
NSDirectoryEnumerator *e = [fileman enumeratorAtPath:tileDirectory];
NSString *path;
int idx = 0;
/*
for (path in e) {
NSString *pathFileName = [NSString stringWithFormat:@"%@/%@/tilemapresource.xml",tileDirectory,[path stringByDeletingPathExtension]];
// NSLog(@"parsing %@",pathFileName);
BOOL checkFile = [self checkForLatLong:pathFileName latitude:f3 longitude:f4 ];
if (checkFile) {
NSLog(@"Match %d",idx);
if (idx < 4){
self.counter.text = [NSString stringWithFormat:@"%d",idx];
[charts insertObject:path atIndex:idx];
idx++;
}
}
}
[path release];
for (int lp=1; lp<[charts count]; lp++) {
NSString *keyName = [NSString stringWithFormat:@"Chart%d",lp+1];
[newManagedObject setValue:[charts objectAtIndex:lp] forKey:keyName];
}
[charts release];
*/
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[error release];
}
[myNumFormatter release];
NSLog(@"Markers Loaded");
[self performSelectorOnMainThread:@selector(dataLoaded) withObject:nil waitUntilDone:YES];
[pool release];
} `
答案 0 :(得分:7)
你可能在这个循环中产生了很多自动释放的对象。请记住,自动释放的对象在整个事件循环结束之前仍然有效,此时自动释放池已耗尽。通常这个过程或多或少是不可见的,但如果你有循环分配大量自动释放的对象,你可以用完堆。
您可以通过在循环中创建和排空内部自动释放池来缓解这种情况,如下所示:
for (line in lines) {
NSAutoreleasePool * localPool = [[NSAutoreleasePool alloc] init];
...
[localPool release];
}
您还应该明确释放您不再需要的任何创建/保留对象,例如上面的“图表”等。
答案 1 :(得分:3)
“chart”可变数组尚未发布。
这一行,
NSMutableArray *charts = [[NSMutableArray alloc] initWithCapacity:4];
答案 2 :(得分:1)
完成后你必须发布图表,如下所示:
[a1 release]
答案 3 :(得分:0)
答案 4 :(得分:0)
在Objective-C中,始终记住此标记行 -
When you use alloc or new or make a copy, you should definitely release it.
那就是你在管理资源而不是编译器。因此,释放它们是你的工作。