libsdl-org / SDL_ttf

Support for TrueType (.ttf) font files with Simple Directmedia Layer.
zlib License
370 stars 124 forks source link

SDL2 TTF_OpenFontRW handling of RWops does not match documentation #317

Closed jabolopes closed 8 months ago

jabolopes commented 10 months ago

Hi,

The SDL2 version of the TTF_OpenFontRW's documentation says the following:

From https://github.com/libsdl-org/SDL_ttf/blob/SDL2/SDL_ttf.h#L238C3-L239C42:

If `freesrc` is non-zero, the RWops will be closed before returning, whether this function succeeds or not.

I suspect this is not true because the function TTF_OpenFontRW will close the RWops if there is an error, but if there's no error the function will not close the RWops and instead it will store it in the TTF_Font structure for later use.

From the same doc paragraph:

 SDL_ttf reads everything it needs from the RWops during this call in any case.

I suspect this is also not true because, as mentioned before, the RWops is stored in the TTF_Font and it is used later.

I spent many hours debugging a crash in my program and I finally found the answer in the following stackoverflow question: https://stackoverflow.com/questions/62032910/getting-a-segfault-using-ttf-rendertext-when-the-font-is-loaded-using-ttf-open

That stackoverflow answer contains a lot more detail regarding the mismatch between the documentation and the implementation.

The solution is to let the RWops remain open as long as the TTF_Font is not closed.

Are there any objections regarding updating the documentation of this function so that it matches the implementation, namely:

Thank you, Jose