C++ std::remove_if/find_if:双重释放或损坏
我定义了以下std::map: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
//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:
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
?