Questions tagged [boost-smart-ptr]

37 questions
37
votes
5 answers

Explicitly deleting a shared_ptr

Simple question here: are you allowed to explicitly delete a boost::shared_ptr yourself? Should you ever? Clarifying, I don't mean delete the pointer held by the shared_ptr. I meant the actual shared_ptr itself. I know most people suggest to not do…
meteoritepanama
  • 6,092
  • 14
  • 42
  • 55
32
votes
5 answers

Is boost shared_ptr thread safe?

I have a question about boost::shared_ptr. There are lots of thread. using namespace boost; class CResource { // xxxxxx } class CResourceBase { public: void SetResource(shared_ptr res) { m_Res = res; } …
user25749
  • 4,825
  • 14
  • 61
  • 83
15
votes
4 answers

boost::shared_ptr and dynamic cast

I have a problem using a shared_ptr of a base class, I can't seem to be able to call the derived class's methods when dereferencing it. I believe code will be more verbose than me: class Base : public boost::enable_shared_from_this { …
HolyLa
  • 153
  • 1
  • 1
  • 4
8
votes
2 answers

Can boost::smart_ptr be used in polymorphism?

Can boost::smart_ptr such as scoped_ptr and shared_ptr be used in polymorphism? class SomeClass { public: SomeClass() { a_ptr.reset(new SubClass); } private: boost::scoped_ptr a_ptr; }
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
8
votes
3 answers

intrusive_ptr: Why isn't a common base class provided?

boost::intrusive_ptr requires intrusive_ptr_add_ref and intrusive_ptr_release to be defined. Why isn't a base class provided which will do this? There is an example here: http://lists.boost.org/Archives/boost/2004/06/66957.php, but the poster says…
Jon
  • 5,275
  • 5
  • 39
  • 51
7
votes
2 answers

How can I take a single element out of a boost library (e.g. shared_pointer)?

I've been playing around with some Boost components, and the only one I see a direct need for in the project I'm working on is boost::shared_ptr. Would it be difficult to just include the required files for shared_ptr, or at least just include files…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
6
votes
1 answer

How to use boost::smart_ptr in polymorphism?

Boost smart pointers can be used with polymorphism, but how do you cast the subclass back to the pointer? using namespace boost; // ... shared_ptr a_ptr(new SubClass); // ... shared_ptr b_ptr = (shared_ptr)a_ptr; //…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
5
votes
3 answers

sort order of boost::weak_ptr after expiring?

For boost::weak_ptr the operator< is defined, so that it can be used in associative containers. My question is: Is the sort order of several weak_ptr objects stable even when some of them change to a refcount of zero? Doesn't that mess with…
Hannah S.
  • 3,081
  • 3
  • 20
  • 27
5
votes
3 answers

how to convert a boost::weak_ptr to a boost::shared_ptr

i have a shared_ptr and a weak_ptr typedef boost::weak_ptr classnamePtr; typedef boost::shared_ptr xPtr; how to convert a weak_ptr to a shared_ptr shared_ptr = weak_ptr; Xptr = classnameptr; ?????
Pinky
  • 1,217
  • 4
  • 17
  • 27
4
votes
1 answer

What is going on here?

This doesn't compile, #include class X { public: void intrusive_ptr_add_ref(X* blah) { } void intrusive_ptr_release(X * blah) { } }; int main() { boost::intrusive_ptr ex(new X); } But this does : #include…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
4
votes
2 answers

using smart pointers with "this"

I'm learning the use of boost smart pointers but I'm a bit confused about a few situations. Let's say I'm implementing a state machine where each state is implemented by a single update method. Each state could return itself or create a new state…
Emiliano
  • 22,232
  • 11
  • 45
  • 59
3
votes
3 answers

Does boost::scoped_ptr violate the guideline of logical constness

In boost::scoped_ptr operator* and operator-> are declared const functions, though they return T& and T* which potentially allows clients to change the underlying data. This violates the idea of logical constness (Myers, Effective C++) Shouldn't the…
nisah
  • 2,478
  • 3
  • 22
  • 20
2
votes
1 answer

Tips on debugging SWIG-wrapped C++ code in Eclipse?

I have a large body of C++ code that I've wrapped with SWIG and am calling it from Java. The C++ code makes liberal use of boost smart pointers. Some of my JUnit tests complete but then experience seg faults during cleanup. The stack trace…
Bill
  • 23
  • 3
2
votes
3 answers

Deleting an object via std::weak_ptr

I have a std::vector with thousands of objects, stored as shared_ptr. Since the object has many attributes that can be used for searching, I maintain multiple indexes to this std::vector on std::map and std::multimap using…
Sharath
  • 1,627
  • 2
  • 18
  • 34
2
votes
1 answer

Is there a generic "clean-up" class in boost?

I simply want a class that does this: class cleanup : boost::noncopyable { public: typedef boost::function0 function; explicit cleanup( function f ) : func( f ) { } ~cleanup() { func(); } private: function func;…
CashCow
  • 30,981
  • 5
  • 61
  • 92
1
2 3