Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Sources] Thread-safe Resource Manager  (Read 19777 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Sources] Thread-safe Resource Manager
« Reply #15 on: May 11, 2010, 08:20:09 am »
Indeed, this kind of strategy (ReleaseUnusedResources) can work only if all images are stored in shared_ptrs. In your situation there's no way to detect which resources are still in use, and which are not. So you shouldn't care about it and keep your images -- and add an explicit RemoveResource function to the manager is necessary.
Laurent Gomila - SFML developer

CBenni::O

  • Newbie
  • *
  • Posts: 48
    • View Profile
[Sources] Thread-safe Resource Manager
« Reply #16 on: May 12, 2010, 11:43:10 am »
Quote from: "Xorlium"
Hi,

I keep having problems with using sfml together with this.

So I can make an image manager. So I have the class ImageManager which inherits from ResourceManager.

Then I make a sf::Sprite sp, and suppose I have a boost::shared_ptr<sf::Image> im and I want to assign that image to the sprite. So I go sp.SetImage(*im). But then I get always the white rectangle, because the use count doesn't increase and the resource manager thinks it has the unique pointer to it, so it deletes it.

What can I do?


I had the same problem,
Either you have to lock every other thread while you load your picture (see the tutorial), or do what I did:

I have a ResourceManager-class with a Resourceloading-thread. It saves different kinds of resources, which all have a Load() and a Get() method.
The Load Method loads the image, despite the fact that it is useless ;)
The first call of Get() runs ths code:
Code: [Select]
MyImage.reset(new sf::Image(*MyImage.get()));
This works out fine ;)

If you want to download the whole Framework, click here
(It is in german, but I hope that works out somehow)

EDIT: Oh, I just saw, does using boost::shared_ptr solve the problem?

bye, CBenni::O
42!
Metal will never die!

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
[Sources] Thread-safe Resource Manager
« Reply #17 on: May 13, 2010, 03:32:58 pm »
Hi,

No, having the shared_ptr doesn't solve the problem. What does is keeping a copy of the shared_ptr somewhere, perhaps in the entity that needs the image. And at the end just release unused resources.

Either that or never release unused resources until the end of the game.

Xorlium