Jump to content

  • Log In with Google      Sign In   
  • Create Account


Smart Pointers and Resource Management (SDL)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 SonicD007   Members   -  Reputation: 457

Like
0Likes
Like

Posted 11 March 2013 - 08:20 AM

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, 11 March 2013 - 08:20 AM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9386

Like
1Likes
Like

Posted 11 March 2013 - 08:33 AM

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);

#3 SonicD007   Members   -  Reputation: 457

Like
0Likes
Like

Posted 11 March 2013 - 09:00 AM

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


#4 Brother Bob   Moderators   -  Reputation: 7774

Like
0Likes
Like

Posted 11 March 2013 - 09:21 AM

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.



#5 SonicD007   Members   -  Reputation: 457

Like
0Likes
Like

Posted 11 March 2013 - 10:06 AM

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?



#6 SiCrane   Moderators   -  Reputation: 9386

Like
0Likes
Like

Posted 11 March 2013 - 05:18 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS