Suppose I have a class Boda:
class Boda {
...
};
And I have a member cydo in this class that I want to be a smart pointer (that is, I want it to get deallocated automatically as soon as the class gets destroyed).
I am using Boost's smart pointers, so I write:
class Boda {
boost::shared_ptr<int> cydo;
public:
Boda...
I have code that uses raw pointers throughout.
It needs to call a method that takes the raw pointer into a shared_ptr.
This method is not under my control, belonging to an external api.
I cannot just pass the pointer to the shared_ptr because when it will be deleted
when the shared_ptr goes out of scope in the method (when the method re...
I have a fairly complex multi threaded application (server) that from time to time will crash due to an assert:
/usr/include/boost/smart_ptr/shared_ptr.hpp:418: T* boost::shared_ptr< <template-parameter-1-1> >::operator->() const [with T = msg::Player]: Assertion `px != 0' failed.
I have been unable to pinpoint the cause and was wonde...
Hi,
I am not sure about a good way to initialize a shared_ptr that is a member of a class. Can you tell me, whether the way that I choose in C::foo() is fine, or is there a better solution?
class A
{
public:
A();
};
class B
{
public:
B(A* pa);
};
class C
{
boost::shared_ptr<A> mA;
boost::shared_ptr<B> mB;
void...
Common pointers allows you to create pointer to pointer:
void foo(Object **o) {}
int main()
{
Object * o = new Object();
foo(&o);
}
Is there an analogous construction for shared_ptr?
void foo(std::shared_ptr <Object> *o) {}
int main()
{
std::shared_ptr <Object> o(new Object());
foo(&o);
}
...
When comparing two variants of pointers (classic/shared_pt) 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 (release) /O2 /MD /GL, W7 Prof, CPU 3.GHZ DualCore
Results:
shared_ptr (C++ 0x00):
N[points] ...
I have seen that a useful way to write a clone method that returns a boost::shared_ptr is to do
class A
{
public:
shared_ptr<A> Clone() const
{
return(shared_ptr<A>(CloneImpl()));
}
protected:
virtual A* CloneImpl() const
{
return(new A(*this));
}
};
class B : public A
{
public:
shared_ptr<B> Clone() const
{
...
As a personal exercise, I want to implement the visitor pattern using shared_ptr. I am familiar with Robert Martin's acyclic visitor paper but find the intrusive nature of the virtual accept() and necessary creation of an {X}Visitor class for each {X} class unpleasant. I like the boost::static_visitor class as it encapsulates all the l...
Sorry if this is explicitly answered somewhere, but I'm a little confused by the boost documentation and articles I've read online.
I see that I can use the reset() function to release the memory within a shared_ptr (assuming the reference count goes to zero), e.g.,
shared_ptr<int> x(new int(0));
x.reset(new int(1));
This, I believe ...
I do receive a shared_ptr from a library call, and pass it and some resource back into the library. The resource can only be deleted when the shared_ptr deletes its pointer:
std::ofstream* out = new std::ofstream();
...
shared_ptr<Lib::SomeClass> writer = Library.createWriter(out);
Library.appendWriter(writer);
The library expects m...
I'm wanting a non-reference counted smart pointer that can combine some of the useful aspects of auto_ptr and shared_ptr. I think that C++0x's unique_ptr is ultimately what I'd need, but I need something that will compile on Visual Studio 2008 and Xcode (gcc 4.2).
The functionality I need is:
Usable in factory methods so that owners...
Somewhere I saw a post about an optimized way of creating a boost shared_ptr so that it allocated the ptr plumbing and the pointee at the same time. I did a SO search but there are a lot of posts on shared_ptr and I could not find it. Can somebody smart please repost it
edit:
thanks for answer. extra credit question. Whats the correct (...
I was recently introduced to the existence of auto_ptr and shared_ptr and I have a pretty simple/naive question.
I try to implement a data structure and I need to point to the children of a Node which (are more than 1 and its) number may change. Which is the best alternative and why:
class Node
{
public:
// ...
Node...
I wanted to make a special version of shared_ptr that would perform specific operations when it was created or destroyed, but my plans appear to be foiled by the realization that shared_ptr's destructor is non virtual, meaning when I override it, my pointers never get cleaned up when the last instance of them are destroyed.
The only al...
Hi all,
I have a question regarding shared_ptrs and ownership in C++:
I have a bunch of objects created on the heap. Each one has a container which holds pointers to some of these objects, and sometimes, even a pointer to the same object the container belongs to. Since I read about the risks of using shared_ptr under such circumstances...
There are several interesting questions on pitfalls with boost::shared_ptrs. In one of them, there is the useful tip to avoid pointing boost::shared_ptr<Base> and boost::shared_ptr<Derived> to the same object of type Derived since they use different reference counts and might destroy the object prematurely.
My question: Is it safe to ha...
Hi!
I'm working on a couple of classes and I'm wondering how I can use a normal member in my application class, where the member needs to use shared_from_this()?
Here is some code to clarify what I mean (see comments)
class Observable {
public:
void addObserver(boost::shared_ptr<Observer> observer) {
// add to a list
}...
Are there any difference between tr1::shared_ptr and boost::shared_ptr? If so, what?
...
I'm trying to use boost::shared_ptr and boost::enable_shared_from_this to no avail. It looks as if shared_from_this() is returning the wrong shared_ptr. Here is what I see:
Task* task = new TaskSubClass();
boost::shared_ptr<Task> first = boost::shared_ptr<Task>(task); // use_count = 1, weak_count = 1
boost::shared_ptr<Task> second = fir...
Hi,
I'm using ACE framework, but I'll try to describe my problem without referencing to it.
I have an event handler (class derived from ACE_Event_Handler).
Reference to the event handler is hold by some manager class in maps of shared_ptr's.
At some point of time I want to:
remove the event handler from manager map
Some method of ev...