简体   繁体   English

C ++ std :: make_shared内存泄漏

[英]C++ std::make_shared memory leak

I'm having memory leak problems with the following line of code: 以下代码行出现内存泄漏问题:

auto state = newSpriteState();

Where these are the related functions: 这些是相关功能:

class SpriteState {
    protected:
        Vector3 position;
        int width, height;
        double rotation, scaling;
        int priority;

    public:
        SpriteState()
            : position(0,0,0),
            width(1), height(1),
            rotation(0), scaling(1.0f),
            priority(0)
        {}

    std::shared_ptr<SpriteState> newSpriteState()
    {
        return std::make_shared<SpriteState>();
    }
};

class Vector3 {
private:
    double x, y, z;

public:
    Vector3( double x_, double y_, double z_ )
    {
        x = x_; y = y_; z = z_;
    }
};

Intel Inspector continues to report that I'm having a memory leak in the function newSpriteState() ; 英特尔检查器继续报告说我的函数newSpriteState()中存在内存泄漏; more specifically std::make_shared<SpriteState>() . 更具体地说std::make_shared<SpriteState>()

UPDATE 更新

Judging from the comments, it seems there may be some external reason for this so here's more code: 从评论来看,似乎有一些外部原因,所以这里有更多代码:

bool Sprite::loadImage() {
    auto state = newSpriteState();
    initStateVector(0, state);
}

where: 哪里:

class Sprite
{
public:
    Sprite();

    std::map<const int, const std::shared_ptr<SpriteState>> stateVector;

    void initStateVector(const int line, std::shared_ptr<SpriteState>& state)
    { 
        stateVector.clear(); 
        stateVector.insert(std::make_pair( line, std::move(state) )); 
    }

    void loadImage();
}

I've uploaded a simplified version of the Sprite class I'm actually using for clarity. 为了清楚起见,我已经上传了我实际上使用的Sprite类的简化版本。

Basically, I'm allocating a shared_ptr<SpriteState> and sticking into a std::map in class Sprite . 基本上,我要分配一个shared_ptr<SpriteState>并坚持使用Sprite类中的std::map

The problem has been solved after an upgrade to vs12. 升级到vs12后,该问题已解决。 My best estimation is that the problem had something to do with the tr1 implementation of smart pointers. 我最好的估计是,问题与智能指针的tr1实现有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM