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.

3323 questions
2
votes
2 answers

Replacing raw pointers in vectors with std::shared_ptr

I have the following structure: typedef Memory_managed_data_structure T_MYDATA; std::vector object_container; std::vector multiple_selection; T_MYDATA * simple_selection; Edit: this may be very important: the…
The Marlboro Man
  • 971
  • 7
  • 22
2
votes
0 answers

g++ program linked against clang++ shared lib with shared_pointers

I've been working on a personal parser for a few month now. At each step, I have verified that it was able to compile and work properly either with clang++ or g++. And it was OK since now ( this project doesn't use any other library than the STL ).…
user2174468
  • 229
  • 1
  • 6
2
votes
2 answers

std::vector::erase() (multithreaded) 'Assertion `px != 0' failed.'

Similar to shared_ptr Assertion px != 0 failed I'm writing a game server that spawns a new thread to handle each user session. The main thread has a std::vector of UserSession shared pointers. Another thread periodically removes dead sessions from…
coffeebean
  • 95
  • 7
2
votes
1 answer

How to convert between shared_ptr to FILE* in C++?

I am trying to use a FILE pointer multiple times through out my application for this I though I create a function and pass the pointer through that. Basically I have this bit of code FILE* fp; _wfopen_s (&fp, L"ftest.txt", L"r"); …
Hossein
  • 24,202
  • 35
  • 119
  • 224
2
votes
2 answers

Use of const pointer to class in function

I am using std::shared_ptr liberally in my code. I have a few functions that I want to call from MyClass using "this" so have declared these functions as (for example) int AnotherClass::foo(const MyClass *obj) { } I want the const to make it clear…
mike
  • 1,192
  • 9
  • 32
2
votes
2 answers

Boost serialization with shared_ptr without implementing serialize() function in pointed class

In the boost tutorial and example of using shared pointers, they have a class A, and they create a shared pointer pointing to an object of class A: boost::shared_ptr spa(new A); Then they serialize it: std::ofstream…
yonigo
  • 987
  • 1
  • 15
  • 30
2
votes
1 answer

"Observer-Pattern" in C++ using Smart Pointer ?

Professionals out there, this is the first time that I'm actually posting a question to this board that helped me out a lot in the past. I'm still a programming novice and have been trying to learn a lot about programming & software development in…
CoderAndi
  • 37
  • 1
  • 2
2
votes
0 answers

Ownership and shared_ptr in dependent classes

In my program I am drawing a block of text to screen and I want to be able to find the text that corresponds to a given pixel. In my architecture there are two classes, the text_view and the draw_device. draw_device is an abstract base class which…
zounds
  • 773
  • 8
  • 17
2
votes
2 answers

How to manage shared object lifetimes with static casts?

I am new to C++ style casts, and need help in understanding how the code below works (this is some dummy code I wrote to understand things). #include #include class A { public: A() : a(1) { std::cout << "Creating…
Lazer
  • 90,700
  • 113
  • 281
  • 364
2
votes
1 answer

Polymorphic allocation of shared_ptr member variable

I have recently attempted to learn how to use std::shared_ptr. When modifying my existing code I've found myself confused when allocating with member variables (outside of an initialisation list). My old code: Class A { Base* member_var; A() { …
rod
  • 3,403
  • 4
  • 19
  • 25
2
votes
2 answers

C++11 shared_pointer constness within stl containers

I have the following problem and I wonder whether there's a better way to solve it: class myObj { public: typedef std::shared_ptr handle; typedef std::shared_ptr const_handle; int someMethod() { ... } int…
2
votes
2 answers

C++ shared_ptr holding dynamically allocated array

I write simple class representing undiricted graph. I would like to have a private class member - pointer to dynamically allocated array of sets. Every set in array represent the vertices adjacent with vertex with corresponding array index number.…
vard
  • 2,142
  • 4
  • 30
  • 40
2
votes
1 answer

Boost Array - conversion to BYTE

So i have this: boost::array data_; How do i convert it to normal BYTE/Char buffer or how do i print the data inside without converting it , using printf? How can i compare it with other normal chracter buffer for example "hello". It will be also…
2
votes
1 answer

Never provide destructor in the PIMPL(using boost scoped_ptr), the g++(4.6.1) doesn't generate compile error, Why?

After I read the reference link: Must provide destructor in the PIMPL, I do the follow the example, but the g++(4.6.1) doesn't generate compile error as I expected The source code is: // Predeclare.h #include #include…
ajaxhe
  • 571
  • 1
  • 5
  • 13
2
votes
2 answers

boost::shared_ptr vs std::tr1::shared_ptr on multi os compilation

I have maintained different code for browser plugin(c++) for windows and mac system. The difference of the code is only for shared pointer. In windows version I am using std::tr1::shared_ptr and on Mac version am using boost::shared_ptr. Now I…
Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133