dll - vc6中STL映射分配错误

标签 dll dictionary access-violation visual-c++

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

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

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

最佳答案

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

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

关于dll - vc6中STL映射分配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16370914/

相关文章:

c# - C++ 将二维数组构造成 C#

c++ - 将 std::string 深度复制到 char* 的正确方法

c# - Marshal.GetDelegateForFunctionPointer 失败

c# - 是否可以使用类库项目中的类子集编译 dll?

python - 使用 python 透视 CSV 字符串,而不使用 pandas 或任何类似的库

c# - System.AccessViolationException : Attempted to read or write protected memory. 这通常表示其他内存已损坏

python - 如何判断字典中是否存在非空字符串?

python - 字典包含文本文件中的单词作为键,所有后续单词的列表作为值

visual-studio-2010 - 应用程序在 Visual Studio 中崩溃,但仅在设置断点之后

visual-c++ - 0xC0000005 - 访问冲突(每次来自不同的 dll)