2

我正在使用地图来合并重复项并对 dll 中的项目进行排序。它没有暴露在 dll 的接口中。简化代码如下。

UINT GetInfo(UINT request, LPVOID data)
{
    //...

    switch (request)
    {
    case COUNT_RES:
    {
        CountRes* countRes = (CountRes*)data;
        ZeroMemory(countRes, sizeof(CountRes));

        try
        {
            //...

            PUINT posValue = (PUINT)buffer;
            PUINT posCount = (PUINT)buffer2;
            FLOAT value; UINT count;
            std::map<FLOAT, UINT, std::greater<FLOAT> > coinMap;    //Access violation
            countRes->rejected = ntohl(posCount[20]);
            for (UCHAR i = 0; i < 20; ++i)
            {
                value = (FLOAT)ntohl(posValue[i]) / 100;
                count = ntohl(posCount[i]);
                coinMap[value] += count;    //Access violation
                countRes->total += value * count;
            }

            //...
        }

        //...
}

GetInfo 函数是从 exe 调用的。显示的代码在声明行引发访问冲突异常。调用堆栈如下。

_heap_alloc_dbg(unsigned int 0x00000018, int 0x00000001, const char * 0x00000000, int 0x00000000) line 394 + 8 bytes  
_nh_malloc_dbg(unsigned int 0x00000018, int 0x00000001, int 0x00000001, const char * 0x00000000, int 0x00000000) line 242 + 21 bytes  
_nh_malloc(unsigned int 0x00000018, int 0x00000001) line 194 + 19 bytes  
operator new(unsigned int 0x00000018) line 24 + 11 bytes  
std::_Allocate(int 0x00000018, char * 0x00000000) line 30 + 9 bytes
std::allocator<unsigned int>::_Charalloc(unsigned int 0x00000018) line 62 + 11 bytes
std::_Tree<float,std::pair<float const ,unsigned int>,std::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >::_Kfn,std::greater<float>,std::allocator<unsigned int> >::_Buynode(...) line 587 + 10 bytes
std::_Tree<float,std::pair<float const ,unsigned int>,std::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >::_Kfn,std::greater<float>,std::allocator<unsigned int> >::_Init() line 461 + 16 bytes
std::_Tree<float,std::pair<float const ,unsigned int>,std::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >::_Kfn,std::greater<float>,std::allocator<unsigned int> >::_Tree<float,std::pair<float const ,unsigned int>,std::ma1aad805f(const std::greater<float> & {...}, unsigned char 0x00, const std::allocator<unsigned int> & {...}) line 162 + 67 bytes
std::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >(const std::greater<float> & {...}, const std::allocator<unsigned int> & {...}) line 57 + 47 bytes
GetInfo(unsigned int 0xffffffff, void * 0x0012f658) line 331 + 25 bytes

如果我声明coinMap超出try范围,则异常发生在插入行。调用堆栈如下。

std::greater<float>::operator()(const float & 1.00000, const float &) line 80 + 37 bytes
std::_Tree<float,std::pair<float const ,unsigned int>,std::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >::_Kfn,std::greater<float>,std::allocator<unsigned int> >::insert(const std::pair<float const ,unsigned int> & {...}) line 222 + 37 bytes
std::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >::insert(const std::pair<float const ,unsigned int> & {...}) line 96 + 45 bytes
std::map<float,unsigned int,std::greater<float>,std::allocator<unsigned int> >::operator[](const float & 1.00000) line 93 + 65 bytes
GetInfo(unsigned int 0xffffffff, void * 0x0012f658) line 337 + 18 bytes

我不知道如何解决这个问题。请帮忙!

4

1 回答 1

2

合法分配代码中的崩溃是 99.99% 的堆损坏问题。

注释掉所有data/countRes引用GetInfo(),看看崩溃仍在引发。如果是,则问题很可能不在您在此处发布的代码范围内。

于 2013-05-04T08:46:35.963 回答