【问题标题】:Trying to learn boost::intrusive Q3 - When storing pointers in ICs, should I use smart_pointer?尝试学习 boost::intrusive Q3 - 在 IC 中存储指针时,我应该使用 smart_pointer 吗?
【发布时间】:2014-11-11 23:40:46
【问题描述】:

我对侵入式容器的理解有了很大进步。我有一个运行“一段时间”的程序,然后在这样的代码行上删除 *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?

【问题讨论】:

  • 问题:为什么要使用侵入式容器?如果您要存储指针,那么一定要使用常规容器。
  • sehe,最大的原因是我喜欢 IC,即不必担心对象存储/引用的所有不同位置的语义。我只是从一个地方删除它,所有其他地方都会自动更新。
  • 我会这么说,我很可能能够使用常规对象而不是指针。我只是看不到让它发挥作用。
  • 那么您确实需要 shared_pointers 或享元(启用跟踪)。您不想仅仅为此使用侵入式容器,因为您会陷入生命周期管理的混乱之中。
  • 好的,您在 IC Q1 中的建议使我得到了这个解决方案。我还看到它们的性能非常高,这是非常可取的。我会研究你的其他建议。你是说sp+ics吗? sps 没有 ic 整洁的功能,我只需要担心删除它们的地方吗?不知道什么是蝇量级...

标签: c++ c++11 boost std intrusive-containers


【解决方案1】:

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

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

【讨论】:

  • @ivan 我会编写一个自定义的erase_remove_if,它在容器上运行,而不是维护上述混乱的循环和业务逻辑。
【解决方案2】:

实际上,使用 Boost Intrusive,您总是担心在哪里引用对象(因为到处都是引用)。

也许你只想要weak_ptr。它看起来好像您正在尝试实现垃圾收集。这是一个使用weak_ptr稍微修改的想法:

Live On Coliru

#include <vector>
#include <map>
#include <boost/weak_ptr.hpp>
#include <boost/make_shared.hpp>
#include <iostream>

using std::string;
using boost::shared_ptr;
using boost::weak_ptr;
using boost::make_shared;

struct Person {
    string name_;
    Person(string name) : name_(move(name)) {}

    using tag = weak_ptr<void>;

    tag track() const { return tracking_tag; }

  private:
    shared_ptr<void> tracking_tag = make_shared<char>('x');
};

template <typename T>
inline bool IsDeleted(weak_ptr<T> const& v) {
    return !v.lock();
}

enum favcolor { red, blue, pink, magenta, beige } favorite_color;

template <typename Map> size_t garbage_collect(Map& map)
{
    size_t collected = 0;
    for (auto it = map.begin(); it!=map.end();)
    {
        if (IsDeleted(it->first))
            ++collected, it = map.erase(it);
        else
            ++it;
    }

    return collected;
}

int main()
{
    std::vector<Person> people;

    for (auto&& name : { "John", "Mike", "Garbarek", "Milou", "Confucius", "Kiplat" })
        people.emplace_back(name);

    struct Properties {
        favcolor favorite_color;
        struct Car { string brand; int year; } vehicle;
    };

    std::map<Person::tag, Properties> associated {
        { people[0].track(), Properties { magenta, { "Chevy", 1986 } } },
        { people[2].track(), Properties { pink,    { "Kia",   2011 } } },
    };

    for (auto& p : people) std::cout << p.name_ << " "; std::cout << "\n";
    std::cout << "Defined properties: " << associated.size() << "\n";

    people.erase(std::remove_if(people.begin(), people.end(), [](Person const& p) { return p.name_[1] == 'o'; }), people.end());

    for (auto& p : people) std::cout << p.name_ << " "; std::cout << "\n";
    std::cout << "Defined properties before garbage collect: " << associated.size() << "\n";

    auto count = garbage_collect(associated);
    std::cout << "Defined properties after garbage collect: " << associated.size() << " (" << count << " collected)\n";
}

输出:

John Mike Garbarek Milou Confucius Kiplat 
Defined properties: 2
Mike Garbarek Milou Kiplat 
Defined properties before garbage collect: 2
Defined properties after garbage collect: 1 (1 collected)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多