I'm trying to write a text editor, and I'm working on getting the source list to work(similar to Coda and TextMate). When I open a reletivly small directory, my app works fine. However, when I open a bigger directory, the app crashes saying that it has run out of memory and is unsafe to call malloc.
Basically what the App is doing right now is populating an NSOutlineView with the files and directories of a directory that the user chooses.
Basically what the App is doing right now is populating an NSOutlineView with the files and directories of a directory that the user chooses.
Code:
- (void)populateSourceListView {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir, valid;
valid = [fileManager fileExistsAtPath:path isDirectory:&isDir];
if(valid && isDir) {
SourceListItem *rootItem = [[SourceListItem alloc] init];
[rootItem setTitle:[path lastPathComponent]];
NSArray *files = [fileManager contentsOfDirectoryAtPath:path error:nil];
for(int i = 0; i < [files count]; i++) {
NSString *file = [files objectAtIndex:i];
// If we are a directory
NSMutableString *newPath = [[NSMutableString alloc] init];
[newPath stringByAppendingFormat:@"%@/%@",path,file];
if ([fileManager fileExistsAtPath:newPath isDirectory:&isDir] && isDir == YES) {
SourceListItem *newNode = [[SourceListItem alloc] init];
[self populateItem:newNode atPath:newPath];
[newNode setTitle:file];
[rootItem addChild:newNode];
[newNode release];
} else {
SourceListItem *newNode = [[SourceListItem alloc] initLeaf];
[newNode setTitle:file];
[rootItem addChild:newNode];
[newNode release];
}
[newPath release];
}
[sourceListContents addObject:rootItem];
[rootItem release];
[files release];
}
}
- (void)populateItem:(SourceListItem*)item atPath:(NSString*)p {
if ([item leaf] == YES) {
return;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *items = [fileManager contentsOfDirectoryAtPath:p error:nil];
for (int i = 0; i < [items count]; i++) {
BOOL isDir;
NSString *file = [items objectAtIndex:i];
NSMutableString *newPath = [[NSMutableString alloc] init];
[newPath stringByAppendingFormat:@"%@/%@",p,file];
// If we are a directory
if ([fileManager fileExistsAtPath:newPath isDirectory:&isDir] && isDir == YES) {
SourceListItem *newNode = [[SourceListItem alloc] init];
[self populateItem:newNode atPath:newPath];
[newNode setTitle:file];
[item addChild:newNode];
[newNode release];
} else {
SourceListItem *newNode = [[SourceListItem alloc] initLeaf];
[newNode setTitle:file];
[item addChild:newNode];
[newNode release];
}
[newPath release];
}
[items release];
}