boost::intrusive_ptr and std::list::iterator

Hello Readers, I have a question regarding boost::intrusive_ptr and std::list::iterator. I have a std::list<boost::intrusive_ptr<CDataset> > which holds my datasets. My question: Is the increment counter of the intrusive_ptr incremented only by doing the following (assigning a iterator to a dataset)? (I could try myself, but I do not know what happens with other compilers/libraries) typedef List std::list<boost::intrusive_ptr<CDataset> >; List list; ... // List holds datasets ... // Get an iterator: List::iterator iter = list.begin(); // Is the Increment counter of the dataset incremented by this action? // Or does the iterator not necessarily keep a pointer to the dataset directly? It would be nice, if somebody could answer this question. In the meantime I try out, what my implementation makes (gcc, linux). Greetings Ernst

Ernst Murnleitner wrote:
Hello Readers,
I have a question regarding boost::intrusive_ptr and std::list::iterator.
I have a std::list<boost::intrusive_ptr<CDataset> > which holds my datasets.
My question: Is the increment counter of the intrusive_ptr incremented only by doing the following (assigning a iterator to a dataset)?
[I assume you mean the "reference counter," not the "increment counter."] Not even by doing that. It is only incremented when an intrusive_ptr is copied or assigned, for example, when the contents of the outer list are changed.
(I could try myself, but I do not know what happens with other compilers/libraries)
typedef List std::list<boost::intrusive_ptr<CDataset> >; List list; .... // List holds datasets .... // Get an iterator: List::iterator iter = list.begin();
// Is the Increment counter of the dataset incremented by this action?
None of the actions shown above will cause any reference counts to be incremented (or decremented).
// Or does the iterator not necessarily keep a pointer to the dataset directly?
It does not neccessarily have any particular internal representation, though in practice it does keep a raw pointer to an intrusive_ptr<CDataset>. But manipulating a raw pointer to a smart pointer doesn't alter any reference counts. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

Hello David,
My question: Is the increment counter of the intrusive_ptr incremented only by doing the following (assigning a iterator to a dataset)?
[I assume you mean the "reference counter," not the "increment counter."]
Not even by doing that. It is only incremented when an intrusive_ptr is copied or assigned, for example, when the contents of the outer list are changed.
Thank you for your answer. Just for verification: the output is count = 1 2 3 4 5 5 6 Greetings, Ernst #include <iostream> #include <vector> #include "boost/intrusive_ptr.hpp" class C { public: C(){}; ~C(){}; void IncRef(){count ++;}; void DecRef(){count --; if (count <= 0) delete this;}; int GetCount(){return count;}; int count; }; typedef boost::intrusive_ptr<C> P; void intrusive_ptr_add_ref(C* c) { c->IncRef(); } void intrusive_ptr_release(C* c) { c->DecRef(); } int main(){ P p1(new C()); std::cout << "count = " << p1->GetCount() << std::endl; P p2 = p1; std::cout << "count = " << p1->GetCount() << std::endl; P p3 = p1; std::cout << "count = " << p1->GetCount() << std::endl; std::vector<P> vec; vec.push_back(p2); std::cout << "count = " << p1->GetCount() << std::endl; P p4 = vec[0]; std::cout << "count = " << p1->GetCount() << std::endl; std::vector<P>::iterator it = vec.begin(); std::cout << "count = " << p1->GetCount() << std::endl; P p5 = *it; std::cout << "count = " << p1->GetCount() << std::endl; return 0; }
participants (2)
-
David Abrahams
-
Ernst Murnleitner