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 6973051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:03:30+00:00 2026-05-27T17:03:30+00:00

I’ve got an API that is called with a file descriptor as an argument,

  • 0

I’ve got an API that is called with a file descriptor as an argument, and it internally stores some state associated with the file descriptor. Then on subsequent calls with the same file descriptor value, the previously generated state can be consulted.

This mostly works, except for the case where the calling code calls my API with a file descriptor, then closes the file descriptor, then allocates a new file descriptor (via socket() or accept() or etc) that ends up having the same integer value as the now-closed one, and then passes that new file descriptor to my API. At that point, my API does the wrong thing, because it erroneously associates the old socket’s state with the new file descriptor.

One solution to this issue would be to force the calling code to notify my API whenever it closes a socket, so that my API would know to delete the associated state…. but I’d prefer not to force the user to do that, since it would be inconvenient for them and they’d be prone to forget to do it anyway.

Therefore, I’m wondering if there is any clever way to tell whether a file descriptor at time T is still associated with the same underlying structures that it was associated with at time (T-x). If I could do that, my API would be smart enough to tell when a file descriptor integer-value had been reused and do the right thing.

FWIW this code is intended to run under MacOS/X and Linux primarily, but the more portable the solution, the better.

  • 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-27T17:03:31+00:00Added an answer on May 27, 2026 at 5:03 pm

    I don’t believe so.

    You’re better off providing wrappers for open/close that return an opaque type back to the callers. (You can also provide a function to get the underlying file descriptor out of that opaque type if necessary.)

    If you don’t want to wrap open/close, you can still use an opaque structure for your APIs (just make a pair of functions to create (with a file descriptor parameter) and release that structure), but indeed, your users will have to remember to release or the app will leak. (But C developer are supposed to know how to do that – malloc/free have been around for a while.)

    Depending on exactly what your library provides, there are probably better alternatives, but this is what I believe is generally done for C APIs.

    Side note: if you expect both your library and the user code to issue reads and writes on those sockets… be careful, that’s real tricky.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the

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.