dealing with NSFileHandles retained by readInBackgroundAndNotify
dealing with NSFileHandles retained by readInBackgroundAndNotify
- Subject: dealing with NSFileHandles retained by readInBackgroundAndNotify
- From: Jeff Harrell <email@hidden>
- Date: Thu, 19 Jun 2003 18:07:24 -0500
This has come up once on the list before, back in 12/2002, but I
couldn't find any responses in the archives.
http://216.239.51.100/search?q=cache:aMaabEaEJg8J:cocoa.mamasam.com/
COCOADEV/2002/10/1/
46499.php+readInBackgroundAndNotify&hl=en&lr=lang_en&ie=UTF-8
It seems like readInBackgroundAndNotify retains its receiver, and never
releases it. I have a little application that listens on an
NSSocketPort with acceptConnectionInBackgroundAndNotify. When a
connection is made, the selector fires, and I look at the userInfo's
NSFileHandleNotificationFileHandleItem. At this point, this object
(which I assign to a local pointer variable) has a retain count of 1.
(That makes sense. All objects have a retain count of at least 1.)
I then pass the pointer to another object, which retains it
(retainCount = 2) and then adds it to an array (retainCount = 3). I add
this second object as an observer of
NSFileHandleReadCompletionNotification notifications, and call
readInBackgroundAndNotify.
Meanwhile, stuff happens.
After I've read all the data from the FileHandle that I want to read, I
remove myself as an observer. Then I do some things with the data that
I read, and finally, after I'm done, I release myself. This calls
dealloc, where I remove the FileHandle from the array (retainCount =
2), and release it myself (retainCount = 1). At that point, when
dealloc ends, the object should be destroyed, right?
Except readInBackgroundAndNotify retains the object. When I call
readInBackgroundAndNotify, the retain count goes up, and *stays* up.
When I get to dealloc, the retainCount is 4, and after I remove it from
the array and release it myself it's still 2. So I'm leaking an
NSFileHandle every time I accept and handle a connection.
Am I doing something wrong? Or, more accurately, failing to do
something right? I guess I could just release the filehandle twice, but
that seems like a horrible idea to me.
Help!
--
email@hidden
http://homepage.mac.com/jharrell
_______________________________________________
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.