Questions tagged [shared-ptr]

Reference counted smart pointer class implementing shared ownership

A shared_ptr is a non-intrusive smart pointer that manages the ownership of a shared resource. Multiple shared_ptr objects can share ownership of the same resource, which will be destroyed automatically when there are no more "non-empty" shared_ptr objects referring to it.

The different versions and implementation commonly used are boost::shared_ptr (the original), std::shared_ptr (included in the C++11 standard) and std::tr1::shared_ptr (from the TR1 report on library extensions, lacks certain features such as aliasing).

The related class template weak_ptr is designed for use with shared_ptr and represents a non-owning reference to a resource that is managed by a shared_ptr. This useful to avoid circular references. A weak_ptr can also be used as a "tracking reference" and converted to a shared_ptr to temporarily assume shared ownership when it is required.

2992 questions
61
votes
5 answers

The cost of passing by shared_ptr

I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function arguments. Consider the following: class Dataset {...} void f( shared_ptr< Dataset const > pds ) {...} void g( shared_ptr< Dataset const…
Artem Sokolov
  • 11,596
  • 4
  • 35
  • 65
58
votes
3 answers

std::shared_ptr initialization: make_shared() vs shared_ptr(new Foo)

What's the difference between: std::shared_ptr p = std::shared_ptr( new int ); and std::shared_ptr p = std::make_shared< int >(); ? Which one should I prefer and why? P. S. Pretty sure this must have been answered already, but I…
Violet Giraffe
  • 29,070
  • 38
  • 172
  • 299
57
votes
3 answers

getting a normal ptr from shared_ptr?

I have something like shared_ptr t(makeSomething(), mem_fun(&Type::deleteMe)) I now need to call C styled function that requires a pointer to Type. How do I get it from shared_ptr?
user34537
57
votes
3 answers

shared_from_this causing bad_weak_ptr

I am trying to keep a list of connected clients in asio. I have adapted the chat server example from the docs (http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp03/chat/chat_server.cpp) and here's the important part of what I ended…
chrisvj
  • 725
  • 1
  • 5
  • 9
56
votes
6 answers

NULL pointer with boost::shared_ptr?

What's the equivalent to the following: std::vector vec; vec.push_back(NULL); when dealing with boost::shared_ptr? Is it the following code? std::vector< boost::shared_ptr > vec; vec.push_back(boost::shared_ptr()); Note: I may push…
Frank
55
votes
2 answers

Error: expected type-specifier before 'ClassName'

shared_ptr circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0))); shared_ptr rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0, Vec3f(1.0f, 1.0f, 0)) ); I'm trying to understand why the above…
zeboidlund
  • 8,829
  • 25
  • 108
  • 176
54
votes
4 answers

Is make_shared really more efficient than new?

I was experimenting with shared_ptr and make_shared from C++11 and programmed a little toy example to see what is actually happening when calling make_shared. As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within…
user1212354
  • 559
  • 1
  • 4
  • 6
54
votes
7 answers

std::shared_ptr thread safety

I've read that "Multiple threads can simultaneously read and write different shared_ptr objects, even when the objects are copies that share ownership." (MSDN: Thread Safety in the Standard C++ Library) Does that mean that changing shared_ptr…
Roee Gavirel
  • 17,192
  • 12
  • 58
  • 82
53
votes
4 answers

Should I use shared_ptr or unique_ptr

I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr or std::unique_ptr. I understand that std::unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively…
Clinton
  • 20,364
  • 13
  • 59
  • 142
53
votes
4 answers

std::shared_ptr: reset() vs. assignment

This is a basic question, but I did not find a previous post about it. The title of the following question sounds like it might be the same question as mine, but the question itself does not match the title: is it better to use shared_ptr.reset or…
AlwaysLearning
  • 5,895
  • 2
  • 16
  • 50
51
votes
3 answers

Segmentation fault when using a shared_ptr for private_key

Updates [X] I discovered this happen when TLS::credentials creds is declared on global scope but if I declare it outside seg fault won't happen. I need it to be global because it helps with caching certificates and that multiple threads can use…
jeffbRTC
  • 1,701
  • 3
  • 16
51
votes
5 answers

Are there any downsides with using make_shared to create a shared_ptr

Are there any downsides with using make_shared() instead of using shared_ptr(new T). Boost documentation states There have been repeated requests from users for a factory function that creates an object of a given type and returns a…
Tobias Furuholm
  • 4,447
  • 3
  • 28
  • 39
50
votes
7 answers

shared_ptr: horrible speed

When comparing two variants of pointers—classic vs. shared_ptr—I was surprised by a significant increase of the running speed of the program. For testing 2D Delaunay incremental Insertion algorithm has been used. Compiler settings: VS 2010…
Ian
  • 607
  • 2
  • 7
  • 7
49
votes
13 answers

What are potential dangers when using boost::shared_ptr?

What are some ways you can shoot yourself in the foot when using boost::shared_ptr? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr?
Frank
  • 58,417
  • 87
  • 227
  • 317
46
votes
16 answers

What's your convention for typedef'ing shared_ptr?

I'm flip-flopping between naming conventions for typedef'ing the boost::shared_ptr template. For example: typedef boost::shared_ptr FooPtr; Before settling on a convention, I'd like to see what others use. What is your convention? EDIT: To…
Emile Cormier
  • 26,080
  • 13
  • 87
  • 116