简体   繁体   English

BOOST_FOREACH在boost :: shared_ptr上崩溃了吗?

[英]BOOST_FOREACH crash on boost::shared_ptr?

Trying todo a simple on a vector of shared_ptr and it crashes? 尝试对shared_ptr向量执行简单操作,然后崩溃? I am suspecting I am doing something obviously wrong? 我怀疑自己做的事情明显不对吗?

std::vector< shared_ptr<SUBCLASS> > m_vecSections;

shared_ptr<SUBCLASS> hoodSection;
BOOST_FOREACH(hoodSection, m_vecSections ){
   //do something
}

I am getting a crash in 我在撞车

 void add_ref_copy()
{
    atomic_increment( &use_count_ );
}

I think your syntax should be: 我认为您的语法应为:

BOOST_FOREACH(shared_ptr<SUBCLASS> hoodSection , m_vecSections ){
   //do something
}

Are you adding or removing elements from m_vecSections inside the BOOST_FOREACH ? 您是否要在BOOST_FOREACH内部的m_vecSections添加或删除元素?

That can lead to undefined behavior because BOOST_FOREACH caches the end iterator (so it'll become invalid if the vector is changed). 这可能导致不确定的行为,因为BOOST_FOREACH缓存了结束迭代器(因此,如果改变向量,它将变得无效)。 See http://www.boost.org/doc/libs/1_52_0/doc/html/foreach/pitfalls.html 参见http://www.boost.org/doc/libs/1_52_0/doc/html/foreach/pitfalls.html

Thankyou guys 感谢大伙们

You had no chance to find it, a call like this... 你没有机会找到它,像这样的电话...

weak_ptr weakHood(shared_ptr(this)); weak_ptr weakHood(shared_ptr(this));

released the m_vecSections and caused the crash... 释放了m_vecSections并导致崩溃...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM