2013-05-04 74 views
2

我正在使用映射合併重複項並對dll中的項進行排序。它沒有暴露在dll的界面中。簡化代碼如下。vc6中的STL映射分配錯誤

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

      //... 
     } 

     //... 
} 

從exe中調用GetInfo函數。顯示的代碼會在聲明行中引發訪問衝突異常。調用堆棧如下。

_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 

如果我聲明coinMaptry範圍的,則例外在插入線路中發生代替。調用堆棧如下。

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 

我不知道如何解決這個問題。請幫忙!

+1

看起來像你用'ZeroMemory'調用破壞堆。檢查'data'中傳遞的實際內存塊大小。你確定exe和dll共享'CountRes'的相同定義嗎? – Rost 2013-05-04 06:07:38

+0

@Rost我看不到'CountRes'與'coinMap'有什麼關係。該地圖不會輸出到exe。數據實際上是通過2個數組輸出到exe文件的,這些數據在代碼中沒有顯示。因爲我瞭解到地圖對象不能跨越dll邊界。 – phoenies 2013-05-04 06:18:00

+0

我確實設法多次運行類似的代碼。我忘了我後來改變了什麼,這是一個悲劇。 – phoenies 2013-05-04 06:24:38

回答

2

合法分配代碼中的崩潰是99.99%的堆損壞問題。

評論全部data/countRes參考GetInfo()並看到崩潰仍在上升。如果是,很可能這個問題不在你在這裏發佈的代碼之外。