【发布时间】:2017-05-28 08:19:52
【问题描述】:
这是一个 C++ 内存管理问题,因此我选择在这里提问。 我正在用 SFML 创建游戏。我以前使用过 SDL,管理我的纹理非常简单,但 SFML 的工作方式不同。
我的项目设置是这样的:
class Game
//create window and initialize game stuff
class Character
/*initializes its own textures and is also responsible for updating itself
as well as drawing itself*/
int main()
/*create an instance of Game -> Create an instance of Character and
runGame. I will implement the drawing and updating properly when I
implement a scene graph and scene nodes but for now updating and drawing is
done via global functions called by the Game Class*/
我面临的挑战是在角色类中为 Sprite 图像使用 sf::Texture 会导致绘制一个白色矩形。所以 Character Sprite 失去了它的 Texture 链接,SFML 只是绘制了一个白色的矩形。为了解决这个问题,我创建了一个原始纹理指针并在 Character 构造函数中对其进行初始化。这有效,Sprite 绘制正确。
//inside character.hpp
sf::Texture *texture;
//in Character constructor
texture= new (sf::Texture);
这意味着我必须管理内存并且代码感觉很难看。
我已经尝试这样做来管理分配的内存(我认为)以防止内存泄漏:
Character::~Character()
{ delete texturePtr; }
但这显然是错误的。 当我退出应用程序时,我还尝试在 main() 末尾删除 character.Texture ,但这也会导致错误。
然后我尝试在 Character 构造函数中使用 uinque_ptr:
std::unique_ptr<sf::Texture> texture(new sf::Texture);
//load Texture from Resource Manager instance
这将绘制一个漂亮的海军蓝黑色矩形而不是 Sprite 纹理。我不确定如何或是否可以声明然后初始化一个 unique_ptr。
我认为我正在泄漏内存,如果是,我该如何正确使用智能指针或正确管理我自己的内存?
当我意识到我说得含糊不清时,我添加了更多细节和代码。此外,我正在查询纹理并使用它来设置 Sprite Rect 尺寸,因此它肯定会被加载,但为了安全起见,我有一个记录器函数来检查我调用的任何资源是否已加载。
【问题讨论】:
-
你没有给我们足够的代码来看看你做错了什么。也就是说,使用
unique_ptr可能是个好主意。如果你的纹理是蓝色的,这听起来像是一个初始化错误。另外,请考虑使用make_unique而不是unique_ptr<...> foo(new whatever)。 -
你说你以前的做法有问题...但从来没有问过。您正在寻找一个您甚至不应该遇到的问题的解决方案。你应该问关于你的第一个问题的问题白色精灵。
-
不要在这里使用'new',也不要使用指针。见官方例子sfml-dev.org/tutorials/2.5/graphics-sprite.php
标签: c++ memory-leaks sfml