【发布时间】:2019-03-17 18:48:06
【问题描述】:
我有一个继承自类案例的类 Block:
class Case {
public:
Case(sf::Vector2f const& pos, sf::IntRect const& textureRect, int type = 1);
protected:
int type_;
sf::Vector2f pos_;
sf::FloatRect hitBox_;
sf::IntRect textureRect_;
};
class Block : public Case {
public:
Block(sf::Texture* const& texture, sf::Vector2f const& pos, sf::IntRect const& textureRect, int const& type = 2);
sf::Sprite& getSprite();
private:
std::shared_ptr<sf::Texture> texture_;
sf::Sprite sprite_;
};
(两个构造函数都很基础,我没有在任何地方使用任何新的)
我有一个 unordered_map 的无序地图来存储我的块:
std::unordered_map<int, std::unordered_map<int, Block>> blocks_;
但是当我尝试删除一个时:
if(blocks_[i%lenght].find((i-i%lenght)/lenght) != blocks_[i%lenght].end())
blocks_[i%lenght].erase((i-i%lenght)/lenght);
我得到了错误:
double free or corruption (out)
我试图打印析构函数,在我得到这个错误之前只调用了 Block 的析构函数。
找了2小时左右,终于在这里问了,谢谢!
【问题讨论】:
-
您是否通过
valgrind运行您的代码或尝试在打开地址清理的情况下进行构建? -
i-i%lenght)/lenght-- 我们不知道这相当于什么。为什么不在使用之前打印出这些值,以便知道它们是否有效? -
这只是在连续内存上恢复 Y 位置的计算,我打印它确实得到了好密钥,没问题。
-
如果没有完整且可验证的示例,就不可能肯定地说,但是在您的 Block 构造函数中,您传递了一个
sf::Texture原始指针,但在 Block 中,您有一个std::shared_ptr到sf::Texture。我猜你有多个不相关的共享指针指向同一个纹理。 -
猜你必须使用你的调试器;这里没有足够的信息来猜测发生了什么 - 例如哪个对象被双重释放。
标签: c++ sfml double-free