RWops - error with the TTF_OpenFontRW... |
riksweeney
|
I have exactly the same problem, freeing the buffer causes my game to crash, so I have to leave it allocated:
It's not a massive problem, since I only ever open the font once and the memory is freed when the program closes, but it does look like a bug in SDL_TTF's RWops. |
|||||||||||||
|
djkarstenv
|
Okay good, so it's not just me, I didn't expect anyone else to come across this error, it was a shot in the dark haha
Anyways thanks for your feedback. It took me hours to figure out why my game was crashing, then I spotted it. You're right, it's no big deal to leave it allocated, my games usually only use one or two fonts max. By the way, when you state font = TTF_OpenFontRW(rw, TRUE, fontSize); , the TRUE argument will release the rw struct from memory automatically. When exactly does it get released, after this call or when u call CloseFont()? The documentation only says it gets released, but they dont say when, do u perhaps know? Thanks |
|||||||||||
|
riksweeney
|
I think it gets released at the end of the function call, but I'd have to check the source code to be certain.
|
|||||||||||
|
djkarstenv
|
okay, thanks
|
|||||||||||
|
J3dz
|
In 'TTF_Font* TTF_OpenFontRW( SDL_RWops *src, int freesrc, int ptsize )' you have set the freesrc parameter to 1. This sets the TTF_Font->freesrc flag and is checked when calling the 'void TTF_CloseFont( TTF_Font* font )' function. By calling 'SDL_RWclose( font->src );' the TTF_CloseFont function releases the memory for the internal SDL_RWops structure. Without that (freesrc=1), you have to call SDL_RWclose yourself ( SDL_RWclose( RW ); in your example ).
Functions like SDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc) are different to the TTF_OpenFontRW by design. The image loading function is allocating the surface, checks about formats and palettes, etc. and then copies the image data to the surface. After that the source SDL_RWops is released by the SDL_LoadBMP_RW function itself, when called with a freesrc parameter that evaluates to true:
Now take a look at the differences in TTF_OpenFontRW:
I hope, this helps you to better understand whats going on, "under the hood". Jedzia |
|||||||||||||||
|
djkarstenv
|
Thanks Jedzia!
This does help understand the workings behind the scenes a little, and I now can see the differences a little clearer. Looking at this block of code :
the font buffer has to be freed after using the font for drawing text as with the image version :
the image buffer can be freed before using the SDL_Surface pointer for drawing images. Now the problem I was having is that all the resource loading was coded in one library file, so i needed some way of passing the buffer pointer to the main program so that (in the case of TTF_Font) i can free the buffer later. I found a solution and I made my own buffer reference that then gets used in the main program to release the buffer data, else I have memory leaks. Your code is quite clear and I will use it for future reference - thanks again! |
|||||||||||||||
|