SDL shared_ptr problems
Hi everyone!
I'm having some trouble with a game I'm developing.
I'm using the SDL framework and everything has been going on smoothly until now! I store all pointers in shared_ptr to eliminate possible memory leaks and since I use shared_ptr I can specify a deleter function which is needed in this case.
e.g
SDL_Surface* surface;
. Load
. Image
. Into
. Surface
shared_ptr<SDL_Surface> pointer(surface, SDL_FreeSurface);
This works extremly good for me but I have a problem with my font class. I have a class that loads fonts and store them in a vector. Then another function is used to generate text with one of the fonts in the vector by taking a index as a parameter and using the font at that index within the vector to generate the text.
Here is my class:
This works great except one small problem. If I run the game with the debugger it gives an exception when I close the game (when all the automatic resource cleaning because all the shared_ptr's take place).
It is the shared_ptr's that holds TTF_Font* that makes it crash, eventhough I handle SDL_Surface* and Mix_Chunk* the exact same way.
The exception is: "Unhandled exception at 0x6fbc2ec4 in Frogger.exe: 0xC0000005: Access violation reading location 0xfeeefef6."
and the debugger stops at:
in the file sp_counted_impl.hpp
The call stack is: > Frogger.exe!boost::detail::sp_counted_impl_pd<_TTF_Font *,void (__cdecl*)(_TTF_Font *)>::dispose() Line 144 + 0x9 byt
so I know it is the shared_ptr's that holds TTF_Font* that is the problem. It is wierd though, since I specify TTF_CloseFont as the "deleter" function for them, which I belive is correct. What's even more strange is that I don't have the same problems with, for an example, my shared_ptr<SDL_Surface>. And like I said, the exception only comes when I run the game with the debugger, otherwise it exits correctly...
I've found a way around this with implementing the following function:
If I call that one before TTF_Quit(); and SDL_Quit(); the exception never comes... But it annoys me a lot since I shouldn't need to do that, and I don't need to do that with other shared_ptr's.. Is there something special with TTF_CloseFont that I am missing?
Very greatfull for any thoughs on this matter!
Best regards
Björn
I'm having some trouble with a game I'm developing.
I'm using the SDL framework and everything has been going on smoothly until now! I store all pointers in shared_ptr to eliminate possible memory leaks and since I use shared_ptr I can specify a deleter function which is needed in this case.
e.g
SDL_Surface* surface;
. Load
. Image
. Into
. Surface
shared_ptr<SDL_Surface> pointer(surface, SDL_FreeSurface);
This works extremly good for me but I have a problem with my font class. I have a class that loads fonts and store them in a vector. Then another function is used to generate text with one of the fonts in the vector by taking a index as a parameter and using the font at that index within the vector to generate the text.
Here is my class:
Code:
#include "FontHandler.h"
// Must declare it here as well, otherwise it won't be initialized and we'll get an
// unresolved external symbol when we try to use the vector
std::vector<shared_ptr<TTF_Font> > FontHandler::fontVector;
bool FontHandler::LoadFont(const char* fileName, int fontSize)
{
TTF_Font* tmpfont = TTF_OpenFont(fileName, fontSize);
if (tmpfont == NULL)
return false;
fontVector.push_back(shared_ptr<TTF_Font>(tmpfont, TTF_CloseFont));
return true;
}
shared_ptr<SDL_Surface> FontHandler::GenerateText(fontIndex index, unsigned char r, unsigned char g, unsigned char b, unsigned char a, const char* text)
{
if (index < 0 || index >= fontVector.size())
return shared_ptr<SDL_Surface>();
SDL_Color fontColor = {r, g, b, a};
SDL_Surface* returnText = TTF_RenderText_Solid(fontVector[index].get(), text, fontColor);
return shared_ptr<SDL_Surface>(returnText, SDL_FreeSurface);
}This works great except one small problem. If I run the game with the debugger it gives an exception when I close the game (when all the automatic resource cleaning because all the shared_ptr's take place).
It is the shared_ptr's that holds TTF_Font* that makes it crash, eventhough I handle SDL_Surface* and Mix_Chunk* the exact same way.
The exception is: "Unhandled exception at 0x6fbc2ec4 in Frogger.exe: 0xC0000005: Access violation reading location 0xfeeefef6."
and the debugger stops at:
Code:
virtual void dispose() // nothrow
{
del( ptr );
}in the file sp_counted_impl.hpp
The call stack is: > Frogger.exe!boost::detail::sp_counted_impl_pd<_TTF_Font *,void (__cdecl*)(_TTF_Font *)>::dispose() Line 144 + 0x9 byt
so I know it is the shared_ptr's that holds TTF_Font* that is the problem. It is wierd though, since I specify TTF_CloseFont as the "deleter" function for them, which I belive is correct. What's even more strange is that I don't have the same problems with, for an example, my shared_ptr<SDL_Surface>. And like I said, the exception only comes when I run the game with the debugger, otherwise it exits correctly...
I've found a way around this with implementing the following function:
Code:
void FontHandler::ResetFonts()
{
for (fontIndex i = 0; i < fontVector.size(); i++)
fontVector[i].reset();
}If I call that one before TTF_Quit(); and SDL_Quit(); the exception never comes... But it annoys me a lot since I shouldn't need to do that, and I don't need to do that with other shared_ptr's.. Is there something special with TTF_CloseFont that I am missing?
Very greatfull for any thoughs on this matter!
Best regards
Björn
| Messages In This Thread |
|
SDL shared_ptr problems - Bearcosta - Mar 28, 2011 05:00 PM
RE: SDL shared_ptr problems - OneSadCookie - Mar 28, 2011, 05:44 PM
RE: SDL shared_ptr problems - Bearcosta - Mar 29, 2011, 08:50 AM
RE: SDL shared_ptr problems - OneSadCookie - Mar 29, 2011, 09:32 AM
RE: SDL shared_ptr problems - Nobody - Mar 29, 2011, 10:45 AM
RE: SDL shared_ptr problems - TomorrowPlusX - Mar 31, 2011, 04:25 AM
RE: SDL shared_ptr problems - Bearcosta - Mar 29, 2011, 04:03 PM
|
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| boost dynamic_pointer_cast, shared_ptr not working as expected | JeroMiya | 2 | 6,533 |
Nov 24, 2008 01:29 AM Last Post: masumbuet |
|

