Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:02:19+00:00 2026-05-29T09:02:19+00:00

I am using NSFileHandle to read data from a socket. This is how I

  • 0

I am using NSFileHandle to read data from a socket.
This is how I am creating the filehandle:

filehandle = [[NSFileHandle alloc] initWithFileDescriptor:sock
closeOnDealloc:YES];

I am doing this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readData:)    name:nil object:filehandle];
[filehandle readInBackgroundAndNotify];

I notice that, readInBackgroundAndNotify is increasing the retainCount of the fileHandle by 1. Hence when I release this filehandle once I am done reading data, the memory is not released. And hence it is trying to read some invalid data and the app is crashing. Please note that this issue is occurring in ios 4.3 but not in ios 5.
I am removing observer before releasing the filehandle. Is there anything else I should be doing before releasing the file handle object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:02:19+00:00Added an answer on May 29, 2026 at 9:02 am

    I notice that, readInBackgroundAndNotify is increasing the retainCount
    of the fileHandle by 1.

    As it likely should as a part of an implementation detail… you are asking the filehandle to effectively be scheduled on the background such that if data were to arrive, it will notify.

    Note that retainCount is useless; it might have increased by 5 or 42 in this situation. It is irrelevant. The bottom line is that you are asking it to do something in the background and until the “stop doing stuff in background” state is reached, the object might likely still be live.

    Hence when i release this filehandle once I am done reading data, the
    memory is not released.

    Have you told the file handle to stop reading in background? Perhaps by closeing it? If not, it might likely continue.

    The difference between iOS 4 and 5 is that there may be logic where the file handle notes that there are no more observers and, thus, stops reading automatically. Again, an implementation detail that you should not count on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm using Apple's PictureSharing/PictureSharingBrowser samples to send and receive data. This uses an
From my main thread I call a selector using [self performSelectorInBackground:@selector(startTask) withObject:nil]; This is
Am writing an application for iphone to read a text file using NSData. NSFileHandle
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using mercurial, I've run into an odd problem where a line from one committer
Using Android TelephonyManager an application can obtain the state of data activity over the
using file_get_contents , I open an Internet URL and get the contents of this
I want to read the contents of an assets library file in iOS NSFileHandle
I want to read some float value one by one from a custom file
i am using NSURLConnection to download file from server and storing it locally as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.