开发者

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 smal开发者_如何学运维l video game?

Thanks


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.


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜