Re: -[NSFileHandle readInBackgroundAndNotify] opens the file again
Re: -[NSFileHandle readInBackgroundAndNotify] opens the file again
- Subject: Re: -[NSFileHandle readInBackgroundAndNotify] opens the file again
- From: Greg Parker <email@hidden>
- Date: Mon, 16 Jul 2012 12:02:17 -0700
On Jul 13, 2012, at 7:46 PM, Rick Mann <email@hidden> wrote:
> I'm using NSFileHandle in an ARC app on OS X Lion to read from a serial port.
>
> Everything works fine 'till I go to close the port. I open the port with POSIX calls, set up some stuff, then instantiate an NSFileHandle with the file descriptor I got from open().
>
> Then I call -readInBackgroundAndNotify, and a second file descriptor gets opened (I see this by using lsof).
>
> That FD gets closed the moment some data comes in and a notification gets posted, but I just go right back and call -readInBackgroundAndNotify again to get the next data.
>
> The problem is when I go to close the port. I still have a pending -readInBackgroundAndNotify, and so the port is opened twice. When I call -closeFile on the NSFileHandle, it closes the FD I initially opened, but leaves the second FD open.
>
> I tried setting the NSFileHandle reference to nil so that ARC would release it, and hopefully call -dealloc on it, but either it's not doing that, or -dealloc doesn't close that second FD either.
>
> In any case, when I go to open that port again, I get an error saying the resource is busy (if I quit my app, all FDs get closed).
>
> This sure seems like a bug to me. Am I doing something wrong?
NSFileHandle dups the file descriptor to avoid bugs where the descriptor is closed from under it. That's expected.
NSFileHandle retains itself during the background operation to avoid being deallocated while it's still in progress. I think the -closeFile call is supposed to cancel the background operations which in turn should release that extra retain, but perhaps something in there is not working correctly.
What does the Allocations instrument say about the retain/release history? If it looks like -readInBackgroundAndNotify is performing an extra retain that doesn't get released then you should file a bug report.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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