std :: map导致内存泄漏?

xcr*_*ypt 3 c++ memory-leaks std

编辑

为了使这篇文章更有建设性,让它可能在将来帮助其他人:

问题是这样的:

std::map<Point2, Prop*> mm;
std::pair<Point2, Prop*> p;

if(Keydown(VK_LBUTTON)) {
     p.first = pos; p.second = new Prop();
     mm.insert(p))
}
Run Code Online (Sandbox Code Playgroud)

因此,即使迭代地迭代并最终释放所有Prop*指针,有时插入也会失败(因为具有相同键的一对可能已经存在于树中).这意味着创建的新Prop()将变为孤立.

解决方案:

1)要么总是使用std :: shared_ptr(可能是最好的解决方案)

2)或这样做:

std::map<Point2, Prop*> mm;
std::pair<Point2, Prop*> p;

if(Keydown(VK_LBUTTON)) {
     p.first = pos; p.second = new Prop();
     if(mm.insert(p).second == false) {
          delete p.second;
     }
}
Run Code Online (Sandbox Code Playgroud)

感谢SO用户parapura rajkumar


原始问题:

我的应用程序中有内存泄漏,我不知道是什么导致它们!我以为我要解除我所要做的一切.奇怪的是:每次我运行我的应用程序时都没有内存泄漏.

简而言之,这就是我的应用程序所做的:

在初始化时,它会创建numRows时间numColumns new Tile()TileList.当鼠标悬停在屏幕上的某些位置,鼠标左键举行,它增加了一个std::pair<Point2, Prop*> p,用p.second = new Prop()一个std::map.

有时我可以添加一大堆道具并退出应用程序而不会有任何泄漏.有时我会像以前一样添加相同的道具,退出时会出现内存泄漏.

请帮忙.这是相关代码:

如果您需要查看我的代码的特定部分,只需对其进行评论,我将编辑该问题

PropList.h

class PropList
{
protected:
    std::map<Point2, Prop*> m_Props_m;

public:
    PropList(){}
    virtual ~PropList();

    bool PropAdd(std::pair<Point2, Prop*> p)
    {
        pair<map<Point2, Prop*>::iterator,bool> ret = m_Props_m.insert(p);
        return ret.second;
    }
    bool PropRemove( const Point2& pos );
    bool HasProp( const Point2& pos );

    void Tick();

protected:

};

static void PropRelease(const std::pair<Point2, Prop*>& p) {
    delete p.second;
}
Run Code Online (Sandbox Code Playgroud)

PropList.cpp

PropList::~PropList()
{
    std::for_each(m_Props_m.begin(), m_Props_m.end(), &PropRelease);
}

bool PropList::PropRemove( const Point2& pos )
{
    std::map<Point2, Prop*>::iterator it = m_Props_m.find(pos);
    if (it == m_Props_m.end()) {
        return false;
    }
    delete (*it).second;
    m_Props_m.erase(it);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

TileList.h

class TileList
{
protected:
    std::vector<std::vector<Tile*> > m_Tiles_v;
    PropList m_PropList;

    UINT m_iRowNum;
    UINT m_iColNum;

public:
    TileList(UINT numColumns, UINT numRows);
    virtual ~TileList();

    //Props
    void PropAdd(std::pair<Point2, Prop*> p);
    void PropRemove(const Point2& pos);
    bool HasProp(const Point2& pos);
    void Tick();

    UINT GetNumRows(){return m_iRowNum;}
    UINT GetNumCols(){return m_iColNum;}

protected:
};
Run Code Online (Sandbox Code Playgroud)

TileList.cpp

TileList::TileList(UINT numColumns, UINT numRows)
    :m_iRowNum(numRows)
    ,m_iColNum(numColumns)
{
    for (UINT i = 0; i < numRows; ++i) {
        m_Tiles_v.push_back(std::vector<Tile*>());
        for (UINT j = 0; j < numColumns; ++j) {
            m_Tiles_v[i].push_back(new Tile());
        }
    }
}

TileList::~TileList()
{
    BOOST_FOREACH(std::vector<Tile*> col_tiles_v, m_Tiles_v)
    {
        BOOST_FOREACH(Tile* pTile, col_tiles_v)
        {
            delete pTile;
        }
    }
}

void TileList::PropAdd(std::pair<Point2, Prop*> p)
{
    if(m_PropList.PropAdd(p)) {
        m_Tiles_v[p.first.y][p.first.x]->setOccupied(true);
    }
}

void TileList::PropRemove(const Point2& pos) 
{
    if(m_PropList.PropRemove(pos)) {
        m_Tiles_v[pos.y][pos.x]->setOccupied(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

par*_*mar 6

我想你的问题可能就在这里

 bool PropAdd(std::pair<Point2, Prop*> p)
    {
        pair<map<Point2, Prop*>::iterator,bool> ret = m_Props_m.insert(p);
        return ret.second;
    }
Run Code Online (Sandbox Code Playgroud)

在您的代码中,mProps似乎拥有Prop*的所有权.但是如果添加一个重复的道具将会泄漏,因为只有Prop可以存在一个唯一的Point2并且map :: insert将失败并且Prop将被孤立.

每当您在map或stl容器中插入指向对象的指针时.尝试将其更改std::shared_ptr为自动内存管理.在你的情况,如果你这样做,无论你的析构函数不必做一个明确的清理和你并不需要static PropRelease在所有

  • 最好的解决方法是使用**shared_ptr**.至少一个upvote怎么样:) (2认同)