Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::remove_if/find_if:双重释放或损坏_C++_C++11_Stdvector_Memory Management - Fatal编程技术网

C++ std::remove_if/find_if:双重释放或损坏

C++ std::remove_if/find_if:双重释放或损坏,c++,c++11,stdvector,memory-management,C++,C++11,Stdvector,Memory Management,我定义了以下std::map: //The map holding the list of registered services per partition key. std::map<SRServicePartitionKey, std::vector<EndPointAddr*>* > mServiceMap; //包含每个分区键的已注册服务列表的映射。 std::map mServiceMap; 我在下面指出了一个函数,它的目标是删除向量中的一个特定End

我定义了以下std::map:

 //The map holding the list of registered services per partition key.
 std::map<SRServicePartitionKey, std::vector<EndPointAddr*>* > mServiceMap;
//包含每个分区键的已注册服务列表的映射。
std::map mServiceMap;
我在下面指出了一个函数,它的目标是删除向量中的一个特定EndPointAddr*指针,该指针保存在上面定义的Map实例的值中。在以下场景实现后,我将在gdb中获得SEGABORT:

  • 将多个项目添加到地图
  • 使用以下功能逐个删除项目
  • 再次添加已删除的项目(其中一些)
  • 删除一项==>此时,我在GDB中得到一个sigabort,并显示以下消息:

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

  • GDB回溯跟踪在底部可用

    问题 您认为下面的删除功能中有哪些特定错误?为什么会出现“双重自由或腐败”错误?您认为我在删除函数中缺少了什么?首先找到要删除的项,然后将其从向量中删除,最后取消分配

    删除功能

    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;
    }
    
    bool
    ServiceRegister::removeService(const EndPointAddr和ePaddrForRemove)
    { 
    bool condErased=假;
    对于(自动it=mServiceMap.begin();it!=mServiceMap.end();++it)
    {     
    std::无法擦除(toBeErasedEP,serviceList->end());
    condErased=正确;
    }   
    如果(TobedAllocatedEP!=0)
    {
    !!!!!!!!!!!!!!!!!1396线在这里!!!!!!!!!!!!!!!
    删除tobedeallocatedp;
    }   
    }//For循环结束
    回报率下降;
    }
    
    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
    
    (gdb)bt
    #0 0x00007FF7026425在/lib/x86_64-linux-gnu/libc.so.6的提升()中
    #1 0x00007ffff7029b8b位于/lib/x86_64-linux-gnu/libc.so.6的abort()中
    #2 0x00007FF706439E英寸??()来自/lib/x86_64-linux-gnu/libc.so.6
    #3 0x00007ffff706eb96英寸??()来自/lib/x86_64-linux-gnu/libc.so.6
    #std::basic_string::~basic_string()()中的4 0x00007ffff7681540来自/usr/lib/x86_64-linux-gnu/libstdc++.so.6
    #5 0x0000000000434604 in EndPointIpAddr::~EndPointIpAddr(this=0x7ffff0002fb0,u in_chrg=)at/home/holb/DESIGN/ECLB\u CP/REPs/V2/ECLB\u CP/src/control\u components//control\u api/api\u util/EndPointIpAddr.hpp:28
    #EndPointAddr中的6 0x0000000000434660::~EndPointAddr(this=0x7ffff0002f90,u in_chrg=)位于/home/holb/DESIGN/ECLB_CP/REPs/V2/ECLB_CP/src/control_components//control_api/api_util/EndPointAddr.hpp:36
    #7 0x000000000043c97f-in-ServiceRegistrator::removeService(this=0x7fffffffdea0,epaddrforremovation=…)位于/home/holb/DESIGN/ECLB_CP/REPs/V2/ECLB_CP/src/control_api/ServiceRegistrator.cpp:1396
    
    您似乎在使用find,但没有检查它是否有效。如果找不到值并且
    find\u如果
    返回
    serviceList->end()
    ,则将取消对
    serviceList->end()的引用


    将此解引用值存储到的变量就是稍后尝试删除它时给您带来麻烦的变量。确实找到了匹配的值吗?

    可能不相关:尽管通过
    const&
    接受
    epaddrforremove
    ,但您在每个lambda中捕获了一个副本。如果复制成本很高,请通过引用捕获它。如果不是,为什么不按值将其传递到
    removeService