gpt4 book ai didi

c++ - 尝试学习 boost::intrusive Q3 - 在 IC 中存储指针时,我应该使用 smart_pointer 吗?

转载 作者:行者123 更新时间:2023-11-30 03:59:33 25 4
gpt4 key购买 nike

我对侵入式容器的理解有了很大的进步。我有一个运行“一段时间”的程序,然后在这样一行代码上删除 *it; (见下文):

....

// : public list_base_hook< void_pointer< ip::offset_ptr<void> > >
class OneDepthPrice : public list_base_hook<link_mode<auto_unlink>> // This is a derivation hook
{
public:
Provider provider;
Price price;
public:
list_member_hook<link_mode<auto_unlink>> member_hook_; // This is a member hook

OneDepthPrice(Provider prov, Price p) : provider(prov), price(p) {}
};

...

std::vector<OneDepthPrice *>& vecPrices

for (auto it = vecPrices.begin(); it != vecPrices.end(); ++it)
{
auto& e = *it;
#if DEBUG
std::cout << e->provider.name << "\n" << std::flush;
#endif
if(e->provider.name == newPrices.provider.name)
{
delete *it; //This is the offending line in the stack trace in the debugger.

it = vecPrices.erase(it);
}
}

程序因堆栈跟踪而崩溃:

#0 0x407ddd boost::intrusive::list_node_traits<void*>::set_next(n=@0x7fffffffe2b8: 0x0, 
next=@0x7fffffffe2b0: 0x706860) (/usr/local/include/boost/intrusive/detail/list_node.hpp:64)
#1 0x409189 boost::intrusive::circular_list_algorithms<boost::intrusive::list_node_traits<void*> >::unlink(this_node=@0x7fffffffe2e8: 0x706830) (/usr/local/include/boost/intrusive/circular_list_algorithms.hpp:140)
#2 0x407e2a boost::intrusive::generic_hook<boost::intrusive::get_list_node_algo<void*>, boost::intrusive::default_tag, (boost::intrusive::link_mode_type)2, (boost::intrusive::base_hook_type)1>::unlink(this=0x706830) (/usr/local/include/boost/intrusive/detail/generic_hook.hpp:180)
#3 0x406b5c boost::intrusive::detail::destructor_impl<boost::intrusive::generic_hook<boost::intrusive::get_list_node_algo<void*>, boost::intrusive::default_tag, (boost::intrusive::link_mode_type)2, (boost::intrusive::base_hook_type)1> >(hook=...) (/usr/local/include/boost/intrusive/detail/utilities.hpp:371)
#4 0x405b13 boost::intrusive::generic_hook<boost::intrusive::get_list_node_algo<void*>, boost::intrusive::default_tag, (boost::intrusive::link_mode_type)2, (boost::intrusive::base_hook_type)1>::~generic_hook(this=0x706830, __in_chrg=<optimized out>) (/usr/local/include/boost/intrusive/detail/generic_hook.hpp:160)
#5 0x40534a boost::intrusive::list_base_hook<boost::intrusive::link_mode<(boost::intrusive::link_mode_type)2>, void, void>::~list_base_hook(this=0x706830, __in_chrg=<optimized out>) (/usr/local/include/boost/intrusive/list_hook.hpp:86)
#6 0x405546 OneDepthPrice::~OneDepthPrice(this=0x706830, __in_chrg=<optimized out>) (/home/idf/Documents/TestCPPArrays/TestCPPArrays.cpp:86)
#7 0x403728 UpdateBunchTogether(vectogether=..., vecPrices=..., newPrices=...) (/home/idf/Documents/TestCPPArrays/TestCPPArrays.cpp:291)
#8 0x404765 main() (/home/idf/Documents/TestCPPArrays/TestCPPArrays.cpp:558)

这是一个奇怪的错误,因为该程序不是多线程,但它运行了一段时间却没有任何问题。我不确定发生了什么,但也许我需要使用 smart_pointers?

最佳答案

将我的评论移至答案。当你这样做时:

for (auto it = vecPrices.begin(); it != vecPrices.end();  ++it)
{
auto& e = *it;
if(e->provider.name == newPrices.provider.name)
{
delete *it; //This is the offending line in the stack trace in the debugger.

it = vecPrices.erase(it);
}
}

您在删除时正确地更新了迭代器,但随后您无条件地递增了它。这很糟糕,原因有二:如果需要,您可能无法删除下一个对象,如果 erase() 返回 end(),那么您只是走过了结尾你的载体。

要安全删除,您需要做:

for (auto it = vecPrices.begin(); it != vecPrices.end();  /* nothing */)
{
auto& e = *it;
if(e->provider.name == newPrices.provider.name)
{
delete *it; //This is the offending line in the stack trace in the debugger.

it = vecPrices.erase(it);
}
else
{
++it; // this is where we increment
}
}

关于c++ - 尝试学习 boost::intrusive Q3 - 在 IC 中存储指针时,我应该使用 smart_pointer 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26876683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com