Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在映射中擦除时崩溃(C+;+;)_C++_Stl - Fatal编程技术网

C++ 在映射中擦除时崩溃(C+;+;)

C++ 在映射中擦除时崩溃(C+;+;),c++,stl,C++,Stl,下面是一部分总是崩溃的代码。我不明白这次车祸的原因。我所做的就是在循环中设置一些指针。如果我评论这个循环,一切都会好的。真奇怪。。底部的代码是崩溃发生的地方。我不知道析构函数叫什么,为什么我的程序会崩溃 typedef std::set<IDrawable**> TDrawableList; typedef std::map<std::string, TDrawableList> THashLoad; // ... THashLoad::

下面是一部分总是崩溃的代码。我不明白这次车祸的原因。我所做的就是在循环中设置一些指针。如果我评论这个循环,一切都会好的。真奇怪。。底部的代码是崩溃发生的地方。我不知道析构函数叫什么,为什么我的程序会崩溃

    typedef std::set<IDrawable**> TDrawableList;
        typedef std::map<std::string, TDrawableList> THashLoad;
//  ...
    THashLoad::iterator itLoad =  gpMapRenderer->m_unloadedCells.find(file.substr(6, file.length()));
        if (itLoad != gpMapRenderer->m_unloadedCells.end())
        {
                TDrawableList::iterator itCells = itLoad->second.begin();
                TDrawableList::iterator itCellsEnd = itLoad->second.end();
                for (; itCells != itCellsEnd; ++itCells)
                {
                        **itCells = (IDrawable*)itHashPics->second.pImg;
                }

                gpMapRenderer->m_unloadedCells.erase(itLoad);  // < --- CRASH
        }
    // ... 
    // _construct.h
    // ...
template <class _Tp>
inline void _Destroy(_Tp* __pointer) {
# if _MSC_VER >= 1010
  __pointer;
# endif    // _MSC_VER >= 1000
# ifdef _STLP_TRIVIAL_DESTRUCTOR_BUG
  typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;
  __destroy_aux(__pointer, _Trivial_destructor());
# else
#  if ( defined (__BORLANDC__) && ( __BORLANDC__ < 0x500 ) )
    __pointer->_Tp::~_Tp();
#  else
    __pointer->~_Tp(); // < ---- CRASH
#  endif
# endif
# ifdef _STLP_DEBUG_UNINITIALIZED
        memset((char*)__pointer, _STLP_SHRED_BYTE, sizeof(_Tp));
# endif
}
下面是我如何添加到m_UnloadedCell的:

void MapRenderer::AddCellToUnloadedList(const std::string& filename, IDrawable** pElement)
{
    THashLoad::iterator itLoad = m_unloadedCells.find(filename);
    if (itLoad != m_unloadedCells.end())
        itLoad->second.insert(pElement);
    else
    {
        std::set<IDrawable**> _set;
        _set.insert(pElement);
        m_unloadedCells.insert(make_pair(filename, _set));
    }
}
ITHASHCELL:

typedef std::map<int, std::vector<Cell2> > THashCells;
THashCells itHashCells;
typedef std::map THashCells;
THashCells和THashCells;
第2单元:

enum eDrawableType
{
    DTYPE_Sprite = 0,
    DTYPE_Animation
};

struct DrawingElement
{
    eDrawableType type;
    IDrawable* pDrawable;
};

struct Cell2
{
    Cell2()
    {
        drawingElement.reserve(10);
    }

    int location;
    int x, y;

    std::vector<DrawingElement> drawingElement;

    MinimapTileInfo minimap_tile_info[10];
};
enum eDrawableType
{
DTYPE_Sprite=0,
DTYPE_动画
};
结构绘图元素
{
eDrawableType;
IDrawable*pDrawable;
};
结构单元2
{
第2单元()
{
提取元素。储备(10);
}
int定位;
int x,y;
std::矢量绘图元素;
小地图信息小地图信息[10];
};

问题似乎在这一行:

**itCells = (IDrawable*)itHashPics->second.pImg;
不在擦除()中。试着把它注释出来看看。
我认为问题在于初始化指向2个向量内的数据结构的指针,尽管我看到在Cell2中保留了10个元素,但不清楚是否有超过10个元素在其中,或者在itHashCells中保留了足够的元素。因此,如果您在任何向量中插入足够的元素,使它们重新分配空间,则会使gpMapRenderer->m_unloadedCells中的指针无效

我建议花点时间重构代码,尽量避免使用指针对指针,或者至少使用不会使它们失效的数据结构。当前状态下的代码无法处理

您还可以重写此代码:

void MapRenderer::AddCellToUnloadedList(const std::string& filename, IDrawable** pElement)
{
    THashLoad::iterator itLoad = m_unloadedCells.find(filename);
    if (itLoad != m_unloadedCells.end())
        itLoad->second.insert(pElement);
    else
    {
        std::set<IDrawable**> _set;
        _set.insert(pElement);
        m_unloadedCells.insert(make_pair(filename, _set));
    }
}

我认为它更具可读性,至少与原件一样有效。

请不要将我们链接到代码可能在几天/几个月内消失的外部网站。而且“所有人!”完全没有必要。我们知道这是给谁的。好的,谢谢你的编辑。
IDrawable
是否有一个
平凡的析构函数
?你能创建一个可编译的例子来说明你的问题吗?我无法使用此代码复制错误。看起来其他地方的内存已损坏,擦除方法不是原因。如何初始化gpMapRenderer->m_UnloadedCell的元素?谢谢。我将进行一些重构。也许事情会变得明朗起来。
**itCells = (IDrawable*)itHashPics->second.pImg;
void MapRenderer::AddCellToUnloadedList(const std::string& filename, IDrawable** pElement)
{
    THashLoad::iterator itLoad = m_unloadedCells.find(filename);
    if (itLoad != m_unloadedCells.end())
        itLoad->second.insert(pElement);
    else
    {
        std::set<IDrawable**> _set;
        _set.insert(pElement);
        m_unloadedCells.insert(make_pair(filename, _set));
    }
}
void MapRenderer::AddCellToUnloadedList(const std::string& filename, IDrawable** pElement)
{
    m_unloadedCells[ filename ].insert( pElement );
}