STD::MAP导致内存泄漏? [英] std::map causing memory leaks?

查看:0
本文介绍了STD::MAP导致内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

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

问题是:

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

if(Keydown(VK_LBUTTON)) {
     p.first = pos; p.second = new Prop();
     mm.insert(p))
}

因此,即使映射最终将迭代并释放所有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;
     }
}

感谢SO用户Parapura Rajkumar


原始问题:

我的应用程序中有内存泄漏,我不知道是什么原因造成的!我以为我要把我必须要做的事都分派出去。奇怪的是:我不会在每次运行应用程序时都出现内存泄漏。

简而言之,这就是我的应用程序的功能:

在初始化时,它在TileList内创建numRows时间numColumnsnew Tile()。 当鼠标悬停在屏幕上的某个位置并按住鼠标左键时,它会将std::pair<Point2, Prop*> p、WITHp.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;
}

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;
}

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:
};

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);
    }
}

推荐答案

我认为您的问题可能出在这里

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

在您的代码中,mProps拥有道具*的所有权。但如果添加了重复的道具,则会泄漏,因为唯一的Point2只能有道具map::Insert将失败,道具将成为孤立的道具。

只要您在map或stl容器中插入指向对象的指针。尝试将其更改为std::shared_ptr以进行自动内存管理。在您的情况下,如果这样做,您的两个析构函数都不必执行显式清理,并且根本不需要static PropRelease

这篇关于STD::MAP导致内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆