0

我定义了以下 std::map :

 //The map holding the list of registered services per partition key.
 std::map<SRServicePartitionKey, std::vector<EndPointAddr*>* > mServiceMap;

我在下面指出了函数,其目的是删除向量中的特定 EndPointAddr* 指针,该指针保存在上面定义的 Map 实例的值中。在实现以下场景后,我在 gdb 中获得了 SEGABORT:

  1. 向地图添加多个项目
  2. 使用以下功能将项目一一删除。
  3. 再次添加那些已删除的项目(其中一些)
  4. 删除一项 ==> 在这一点上,我在 GDB 中收到一个 sigabort,并显示以下消息:

    * 检测到 glibc * /home/holb/DESIGN/ECLB_CP/REPs/V2/eclb_cp/build_output/ServiceRegistrar:双重免费或损坏(fasttop):0x00007ffff0002e10 *

GDB Back trace 在底部可用...

问题 您认为下面的删除功能有哪些特别的问题?为什么我会收到此“双重免费或损坏”错误?您认为我在删除功能中缺少什么,我首先找到要删除的项目,然后将其从向量中删除,最后释放它。

移除功能

bool
ServiceRegistrar::removeService(const EndPointAddr & epAddrForRemoval)
{ 
  bool condErased = false;

  for(auto it = mServiceMap.begin(); it != mServiceMap.end(); ++it)
  {     
      std::cout << "\tPartition [" 
                    << (*it).first.getInstanceNo() << ","
                    << (*it).first.getContext() << ","
                    << (*it).first.getVersion() << "]:"
                    << std::endl;

      std::vector<EndPointAddr*> * serviceList = (*it).second;  

      auto found = 
            std::find_if(serviceList->begin(),
                         serviceList->end(),
                         [epAddrForRemoval]( EndPointAddr* otherEPAddr ) 
                         { 
                          const EndPointTipcAddr & tipcAddrToRemove = epAddrForRemoval.getImmutableTipcAddress();
                          const EndPointTipcAddr & otherTipcAddr = otherEPAddr->getImmutableTipcAddress();                                                                                          
                          return (tipcAddrToRemove.compareTo(otherTipcAddr));                   
                         });      

     EndPointAddr * toBeDeAllocatedEP = *found;   

     auto toBeErasedEP = 
         std::remove_if(serviceList->begin(),
                        serviceList->end(),
                        [epAddrForRemoval]( EndPointAddr* otherEPAddr ) 
                        { 
                          const EndPointTipcAddr & tipcAddrToRemove = epAddrForRemoval.getImmutableTipcAddress();
                          const EndPointTipcAddr & otherTipcAddr = otherEPAddr->getImmutableTipcAddress();                                                                                          
                          return (tipcAddrToRemove.compareTo(otherTipcAddr));                   
                 });

    if(toBeErasedEP != serviceList->end())
    {         
      serviceList->erase(toBeErasedEP, serviceList->end());    
      condErased = true;
    }   

    if(toBeDeAllocatedEP != 0)
    {
      !!!!!!!!!!!!LINE 1396 is HERE!!!!!!!!!!!!!!!
      delete toBeDeAllocatedEP;
    }   

  } //end of For Loop

  return condErased;
}

GDB 回溯

(gdb) bt
#0  0x00007ffff7026425 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x00007ffff7029b8b in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2  0x00007ffff706439e in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#3  0x00007ffff706eb96 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
#4  0x00007ffff7681540 in std::basic_string<char, std::char_traits<char>,     std::allocator<char> >::~basic_string() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x0000000000434604 in EndPointIpAddr::~EndPointIpAddr (this=0x7ffff0002fb0, __in_chrg=<optimized out>) at   /home/holb/DESIGN/ECLB_CP/REPs/V2/eclb_cp/src/control_components/../control_api/api_util/EndPointIpAddr.hpp:28
#6  0x0000000000434660 in EndPointAddr::~EndPointAddr (this=0x7ffff0002f90, __in_chrg=<optimized out>) at /home/holb/DESIGN/ECLB_CP/REPs/V2/eclb_cp/src/control_components/../control_api/api_util/EndPointAddr.hpp:36
#7  0x000000000043c97f in ServiceRegistrar::removeService (this=0x7fffffffdea0, epAddrForRemoval=...) at /home/holb/DESIGN/ECLB_CP/REPs/V2/eclb_cp/src/control_api/ServiceRegistrar.cpp:1396
4

1 回答 1

1

您似乎在使用 found 而没有检查它是否有效。如果您无法找到一个值并find_if返回serviceList->end(),那么您将取消引用serviceList->end().

您将此取消引用的值存储到的变量是稍后在您尝试删除它时给您带来麻烦的变量。你确定你真的找到了一个匹配的值吗?

于 2013-06-18T19:01:55.013 回答