したがって、この NSFileHandle/NSPipe がトリガーされると、この問題が発生します... CPU の使用とメモリが狂い始めます。問題は、このリークを見つけるのが難しいことです。アドバイスやヘルプをいただければ幸いです。乾杯。
.h
NSTask *task;
NSPipe *pipe;
NSFileHandle *fileHandle;
@property (weak) IBOutlet NSTextField *commandInputTextField;
@property (unsafe_unretained) IBOutlet NSTextView *nsTastOutput;
@property (weak) IBOutlet NSButton *useOutputSwitch;
.m
- (id)init
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(readPipe:)
name:NSFileHandleReadCompletionNotification
object:nil];
}
- (void)tasker
{
task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
NSArray *argumentBuilder = [[_commandInputTextField stringValue] componentsSeparatedByString:@" "];
[task setArguments:argumentBuilder];
// Pipe output to ScrollView
if (_useOutputSwitch.state == YES)
{
if (!pipe)
{
pipe = [[NSPipe alloc] init];
}
fileHandle = [pipe fileHandleForReading];
[fileHandle readInBackgroundAndNotify];
[task setStandardOutput:pipe];
[task setStandardError:pipe];
}
// Launch task
[task launch];
}
- (void)readPipe:(NSNotification *)notification
{
NSData *data;
NSString *text;
if( [notification object] != fileHandle )
{
return;
}
data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSScroller *scroller = [[_nsTastOutput enclosingScrollView] verticalScroller];
BOOL shouldScrollToBottom = [scroller doubleValue] == 1.0;
NSTextStorage *ts = [_nsTastOutput textStorage];
[ts replaceCharactersInRange:NSMakeRange([ts length], 0) withString:text];
if (shouldScrollToBottom)
{
NSRect bounds = [_nsTastOutput bounds];
[_nsTastOutput scrollPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
}
if (data != 0)
{
[fileHandle readInBackgroundAndNotify];
}
}