jmasterx
jmasterx

Reputation: 54133

Is this an acceptable way of managing resources?

To manage various resources in my game (fonts bitmaps etc), I want to create managers which map strings to whatever resource. And this object, when out of scope, frees all memory used by the resources.

So basically if you want a Font, you'd either add it or request with a string indicating its name and it would return a pointer to it. This way the user never has to manage any memory.

Is this a good design for a small video game?

Thanks

Upvotes: 0

Views: 129

Answers (4)

Mark Ingram
Mark Ingram

Reputation: 73673

You might want to consider a boost::intrusive_ptr so that all your objects are ref counted. You can then perform manual disposal of them from within the manager should you need to.

Of course you can write your own smart pointer classes, or you can just take it from someone who already has:
http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/smart_ptr.htm

Upvotes: 0

user2100815
user2100815

Reputation:

There is a problem here if your manager returns ordinary pointers - resources will hang around until the manager itself is destroyed (which is inefficient, particularly for resources like bitmaps), or until you make an explicit call to the manager to get rid of them, which may not happen if exceptions are thrown. It would be better for the manager to return smart pointers of some sort that can inform the manager when the resource is finished with.

Upvotes: 2

Šimon Tóth
Šimon Tóth

Reputation: 36441

What you are describing is a the Factory Pattern combined with Smart Pointer/Scoped Pointer.

It is pretty reasonable. Just consider if you really need to use a string for identifying the resource. An enum might be enough.

Upvotes: 2

Josh
Josh

Reputation: 12566

It sounds kind of like a smart pointer, if it automatically deallocates itself when it goes out of scope.

Upvotes: 0

Related Questions