TTF_OpenFontIndexDPI leaves open file descriptors

I was having a problem in which TTF_OpenFont was sometimes returning NULL with parameters that had previously worked. I finally debugged into the code to see what was going on. In a couple of steps, it calls TTF_OpenFontIndexDPI, which is as follows:

TTF_Font* TTF_OpenFontIndexDPI( const char *file, int ptsize, long index, unsigned int hdpi, unsigned int vdpi )
{
    SDL_RWops *rw = SDL_RWFromFile(file, "rb");
    if ( rw == NULL ) {
        return NULL;
    }
    return TTF_OpenFontIndexDPIRW(rw, 1, ptsize, index, hdpi, vdpi);
}

I think I see a problem here. This function calls SDL_RWFromFile which creates an open file descriptor which it never closes. Do this often enough, SDL_RWFromFile starts returning NULL. Sure looks to me like a failure to close a resource, and eventually the system runs out of file descriptors. Please tell me I’m wrong, but this sure is what it looks like.

I solved my problem for the moment by setting up a caching scheme so that I will never call TTF_OpenFont with the same parameters twice. But this seems a bit unclean.

It leaves it open so that further reading can be done. The RWops gets closed when the font is closed with TTF_CloseFont().

Why are you opening so many fonts at once that running out of file descriptors is an issue?

I’m sorry, I didn’t realize that a font is a live object that needs to be closed. So I wasn’t being careful with them. My bad.