我最终能够使用仪器找出根本原因。我整理了
文件属性
仪器的事件列表,并且能够看到同一文件描述符有许多“打开”事件(以及同一文件和不同描述符的一些事件),没有任何“关闭”事件。我能够找到我使用的库中的违规代码。我在
unit tests
,使用此方法并在运行某些代码之前和之后进行比较:
- (NSInteger)numberOfOpenFileHandles {
int pid = [[NSProcessInfo processInfo] processIdentifier];
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *file = pipe.fileHandleForReading;
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/usr/sbin/lsof";
task.arguments = @[@"-P", @"-n", @"-p", [NSString stringWithFormat:@"%d", pid]];
task.standardOutput = pipe;
[task launch];
NSData *data = [file readDataToEndOfFile];
[file closeFile];
NSString *lsofOutput = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
return [lsofOutput componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]].count;
}