• Advertisement
Sign in to follow this  

Smart Pointers and Resource Management (SDL)

This topic is 1775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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];
}
Edited by SonicD007

Share this post


Link to post
Share on other sites
Advertisement
You'd want to use std::shared_ptr<SDL_Surface>, not std::shared_ptr<SDL_Surface *>. 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<SDL_Surface> foo_ptr(SDL_LoadBMP(....), SDL_FreeSurface);

Share this post


Link to post
Share on other sites

Not quite sure how to do this exactly

 

std::shared_ptr<SDL_Surface> 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<SDL_Surface> 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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
get() is one way. Another option is &*mySharedPtr.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement