(Mis?)Usage of NSFileHandle
(Mis?)Usage of NSFileHandle
- Subject: (Mis?)Usage of NSFileHandle
- From: joie(mecha : himitsu) <email@hidden>
- Date: Fri, 16 Aug 2002 01:15:16 -0500
I've a problem with loading many files using NSFileHandle(or NSFileManager;
both cause the exact same problem which might indicate a problem in my
code, though it doesn't seem like it). Here's the idea:
I've a document-based application(a crack at a refactoring browser like
Squeak Smalltalk's browser app) whose -(BOOL)readFromFile:(NSString
*)fileName ofType:(NSString *)docType likes to take a .pbproj and not
really read it, instead using an NSDirectoryEnumerator and finding all the
sources in the project directory. It then parses them for class/instance
variable/method names and their locations, and packages them all into
nodes in an NSBrowser. It works flawlessly for small projects.
However... upon attempting to read the 43rd file(always the 43rd file,
regardless of whether all the files are in one project; the same small
project can be opened and closed repeatedly and it will cause the same
crash), the +[NSFileHandle fileHandleForReadingAtPath:] method, as well as
-[NSFileManager contentsAtPath:], return nil. It's a preposterous answer;
I NSLog() and find out that yes, -[NSFileManager fileExistsAtPath:] is
true. So, this source code file might be invalid. But it's not as easy
as that! It doesn't matter WHAT file is the 43rd, I consistently(in
absolutely every case) fail to read the 43rd file, causing my app to
crash. The reading code looks like this:
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType
{
id aFile;
NSDirectoryEnumerator * projectEnumerator;
NSString*path;
if([[fileName pathExtension] isEqualToString:@"pbproj"])
{
NSLog(@"%@", fileName);
path = [fileName stringByDeletingLastPathComponent];
projectEnumerator = [[NSFileManager defaultManager]
enumeratorAtPath:path];
while(aFile = [projectEnumerator nextObject])
{
NSAutoreleasePool*pool=[NSAutoreleasePool new];
if(![[aFile pathExtension] isEqualToString:@"pbproj"])
{
if([[aFile pathExtension] isEqualToString:@"h"])
{
[self addHeader:[path
stringByAppendingPathComponent:aFile]];
}
else if([[aFile pathExtension] isEqualToString:@"m"] &&
![aFile isEqualToString:@"main.m"])
{
[self addImplementation:[path
stringByAppendingPathComponent:aFile]];
}
}
[pool release];
}
}
}
both -addHeader and -addImplementation call a method which does
this(theFile is the absolute path string to the appropriate file; i
verified that it's A-OK using both manual inspection and -[NSFileManager
fileExistsAtPath:]):
NSFileHandle * fileHandle = [NSFileHandle
fileHandleForReadingAtPath:theFile];
NSData * data = [fileHandle readDataToEndOfFile];
And, reliably, on the 43rd invocation of this method, the creation of the
fileHandle fails silently(until the nil value of data eventually causes a
crash). (If you were wondering, my app is only registered for ".pbproj",
".h", and ".m")
I'm not running out of available file handles, i think, because of that
autorelease pool...
I've been banging my head against the wall all night; can somebody please
give me a hand?
--joie
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.