Skip to main content
GameDev.net gamedev.net
🔒 Locked

Smart Pointers and Resource Management (SDL)

Started by SonicD007 Mar 11, 2013 at 2:20 PM 4 replies 4.3k views
Original Post
SonicD007
SonicD007

Hey everyone, I'm trying to create a simple resource management system and I would look someone to look over this small snippet of code to make sure I coded this correctly, as it is my first time using boost::shared_ptr. I would also like to know when I would use SDL_FreeSurface() or how would I pass it to the shared pointer to do it for me in regards to resource management. I know I need to somehow make it so I can load any resource but for now I want to test everything using just SDL_Surface*. Thanks!


//In the ResourceController.h file, this private variable is declared
std::map<std::string, std::shared_ptr<SDL_Surface*>> textures;
//////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
bool ResourceController::LoadResource(std::string filePath, std::string keyName)
{
SDL_Surface* temp = SDL_LoadBMP(filePath.c_str());
std::shared_ptr<SDL_Surface*> foo(&temp); //I think this is correct.  I'm pointing to the address of the temp pointer which points to the resource in memory
if (temp)
{
textures.insert(std::make_pair(keyName, foo));
return true;
}
return false;
}
 
std::shared_ptr<SDL_Surface*> ResourceController::GetResource(std::string keyName)
{
return textures[keyName];
}
SiCrane
SiCrane
You'd want to use std::shared_ptr, not std::shared_ptr. The latter gives you a smart pointer to a regular pointer rather than a smart pointer to a SDL_Surface. To set a deleter function you pass it as the second argument to the shared_ptr constructor. Ex: std::shared_ptr foo_ptr(SDL_LoadBMP(....), SDL_FreeSurface);
SonicD007
SonicD007

Not quite sure how to do this exactly

std::shared_ptr foo(SDL_LoadBMP(filePath.c_str()), SDL_FreeSurface(foo));

That's what I have so far but since foo is being declared here, foo is undefined in the second parameter. I was thinking of doing something like

SDL_Surface* temp = SDL_LoadBMP(filePath.c_str());

std::shared_ptr foo(*temp, SDL_FreeSurface(temp));

but I'm not sure if this would be a correct use of shared_ptr nor can I compile that because it says


 
4 IntelliSense: no instance of constructor "std::tr1::shared_ptr<_Ty>::shared_ptr [with _Ty=SDL_Surface]" matches the argument list c:\users\sonicd007\documents\visual studio 2010\projects\prototypes\resource management\resourcecontroller.cpp 17
Brother Bob
Brother Bob

That's not what SiCrane posted. Look again at his usage of the smart pointer; he's not calling SDL_FreeSurface, he's passing the function pointer to it as the second parameter to the shared pointer constructor.

SonicD007
SonicD007

Ah ok. I got it. One last question (assuming everything else looks good) Since SDL_BlitSurface takes SDL_Surface* as a parameter, I need to use mySharedPtr.get() to get a raw pointer to pass into the parameter right?

SiCrane
SiCrane
get() is one way. Another option is &*mySharedPtr.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.