Re: Problems with repetetive execution of netstat using NSTask and NSTimer
Re: Problems with repetetive execution of netstat using NSTask and NSTimer
- Subject: Re: Problems with repetetive execution of netstat using NSTask and NSTimer
- From: "Adam R. Maxwell" <email@hidden>
- Date: Mon, 05 Apr 2010 20:00:28 -0700
On Apr 5, 2010, at 3:02 PM, Ken Thomases wrote:
> On Apr 5, 2010, at 4:05 PM, Kazior Fukacz wrote:
>
>> When I run it, it keeps working perfectly fine for a few minutes, then stops refreshing the IP and prints:
>>
>> IPShowX[14917] *** NSTimer discarding exception '*** -[NSCFDictionary setObject:forKey:]: attempt to insert nil value' that raised during firing of timer with target 34e410 and selector 'getGameIP:'
>>
>> every 3 seconds (what's my NSTimer's interval). It's my first Cocoa application and I don't really understand the problem.
>
> Well, I'm not sure how much you do understand.
>
> Cocoa container classes can't contain 'nil' values; they can only contain actual objects.
>
> Something, somewhere is attempting to insert a nil value into a NSMutableDictionary-derived object. This causes an exception to be raised.
I've seen this before with NSTask, typically when you run out of file descriptors. In a test app I just threw together, the exception is raised because NSTask is blindly trying to insert a nil NSFileHandle in an internal dictionary (the key is _NSTaskOutputFileHandle).
If NSTask asserted that it requires a non-nil NSPipe/NSFileHandle, you'd get a much more useful error, but you can catch at least some of these by asserting that pipe != nil yourself.
[...]
> I suspect you may be getting output from netstat that is not in pure ASCII. Then, 'string' will be nil and that will cascade through so that IPSubstr is nil and IPSplit is nil. Then, you're passing a nil value to [IPField setStringValue:].
Probably not in this case; -[NSString initWithData:encoding:] will always succeed when you use NSASCIIStringEncoding, unfortunately.
> By the way, are you using garbage collection? If not, then you're leaking several objects (those pointed to by 'netstat', 'pipe', and 'string').
Yeah, I suspect that the pipes (and corresponding NSFileHandles) are indeed leaking, and that's the real problem. You may also want to create and launch NSTask instances inside an exception handler, since it and NSFileHandle/NSPipe can raise some unexpected exceptions.
--
Adam
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden