地图清除抛出异常

时间:2019-05-13 08:03:47

标签: c++ visual-c++ dynamic-memory-allocation

我有一个地图和过程来添加这样的数据:

interface CDBColumnInfo
      : public IDBColumnInfo
    {
    public:
        CDBColumnInfo();
        ~CDBColumnInfo();
   private:
    map<int,_bstr_t> m_rowIndexDataMap ;

   HRESULT AddDataToMap(){
    _bstr_t record;
    for (int rownum = 0; rownum < num_rows; ++rownum){
        const int num_cols = PQnfields(res);
        record = "";
        for (int colnum = 0; colnum < num_cols; ++colnum) {
            if (PQgetisnull(res, rownum, colnum) == 0) {
                _bstr_t data = PQgetvalue(res, rownum, colnum);
                record = record + data;
            }
            else {
                record = record + "<NULL>";
            }

            if (colnum + 1 < num_cols) {
                record = record + "#";
            }
        }
        m_rowIndexDataMap[rownum] = record;             
    }

  }

程序完成后,我通过以下命令清除析构函数中的映射:

CDBColumnInfo::~CDBColumnInfo() {
 m_rowIndexDataMap.clear();
}

析构函数完成后我得到了异常:

File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
Line: 904

Expression: _CrtIsValidHeapPointer(block)

我试图手动循环映射并确定在第一个元素中发生了异常:

for (auto it = m_rowIndexDataMap.cbegin(); it != m_rowIndexDataMap.cend() ;)
{       

    it = m_rowIndexDataMap.erase(it);    //  exception happened at first element

}
m_rowIndexDataMap.clear();

地图中的数据怎么了?

1 个答案:

答案 0 :(得分:1)

您收到的错误:

File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 904

Expression: _CrtIsValidHeapPointer(block)

不一定是由“实际”代码或调用堆栈引起的,但很可能是代码中的其他地方,因为它可能是由对堆的例行检查引起的:

  

堆检频率宏

     

您可以指定C运行时库执行验证的频率   调试堆(_CrtCheckMemory)的数量基于对   malloc,realloc,free和_msize。

有关更多信息,请参见_CrtSetDbgFlag