shared-ptr

Passing a shared_ptr through a C interface that takes void *

╄→гoц情女王★ 提交于 2019-12-07 03:52:19
问题 I have a C++ project that uses SDL, in particular SDL Events. I would like to use the event system for incoming network messages just as it is used for UI events. I can define a new event type and attach some arbitrary data (see this example). This is what I would do if I was using ordinary pointers: Uint32 message_event_type = SDL_RegisterEvents(1); /* In the main event loop */ while (SDL_Poll(&evt)) { if (evt.type == message_event_type) { Message *msg = evt.user.data1; handle_message(msg);

boost::python and set::erase -> weird behaviour

限于喜欢 提交于 2019-12-07 03:10:15
问题 I'm trying to store objects in a std::set. Those objects are boost::shared_ptr<>, coming from the python environment. adding values to the set won't cause any troubles. But when I try to erase a value, even though I'm passing the very same reference, it won't work. Here is an example : #include <set> #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/python.hpp> using namespace std; using namespace boost; using namespace boost::python; struct Bar { Bar() {} }; struct Foo {

Soft (not: weak) references in C++ - Is it possible? Is there an implementation?

最后都变了- 提交于 2019-12-07 03:03:51
问题 In C++ I'm using boost::shared_ptr and boost::weak_ptr to automatically delete objects that are no longer needed. I know these work with reference counting. In Java, memory is managed by a garbage collector, which consideres the built-in object references as strong , WeakReference as weak and SoftReference as something in between (may be collected by the GC, but may as well survive the GC), which is really handy for caching objects for some time, but throwing them away as soon as free memory

Are weak pointers guaranteed to have expired by the time the std::shared_ptr deleter runs?

一笑奈何 提交于 2019-12-07 01:19:36
问题 If I have a std::shared_ptr<Foo> with a custom deleter, is it guaranteed that all associated weak pointers are seen as expired by the deleter? (I would appreciate it very much if you could cite relevant sections in the standard.) In other words is the assertion below guaranteed not to fire? std::weak_ptr<Foo> weak; std::shared_ptr<Foo> strong{ new Foo, [&weak] (Foo* f) { assert(weak.expired()); delete f; }, }; weak = strong; strong.reset(); 回答1: The standard guarantees nothing. For shared_ptr

testing if a shared_ptr is NULL

末鹿安然 提交于 2019-12-06 19:16:50
问题 I have the following code snippet: std::vector< boost::shared_ptr<Foo> >::iterator it; it = returnsAnIterator(); // often, it will point to a shared_ptr that is NULL, and I want to test for that if(*it) { // do stuff } else // do other stuff Am I testing correctly? The boost docs say that a shared_ptr can be implicitly converted to a bool, but when I run this code it segfaults: Program received signal SIGSEGV, Segmentation fault. 0x0806c252 in boost::shared_ptr<Foo>::operator Foo* boost:

Is a shared_ptr's deleter stored in memory allocated by the custom allocator?

自闭症网瘾萝莉.ら 提交于 2019-12-06 16:57:05
问题 Say I have a shared_ptr with a custom allocator and a custom deleter. I can't find anything in the standard that talks about where the deleter should be stored: it doesn't say that the custom allocator will be used for the deleter's memory, and it doesn't say that it won't be. Is this unspecified or am I just missing something? 回答1: util.smartptr.shared.const/9 in C++ 11: Effects: Constructs a shared_ptr object that owns the object p and the deleter d. The second and fourth constructors shall

constructing templated containers that are themselves shared_ptr types

我怕爱的太早我们不能终老 提交于 2019-12-06 16:13:10
问题 I'm having some difficulty wrapping my head around this one, likely because I don't fully understand the underlying mechanics. I have a struct like the following: template <class T> struct A { int id; T x; } This struct is then used as a type for a shared_ptr: typedef std::shared_ptr<A> A_ptr; Which in turn gets stored in a map: typedef unordered_map<int , A_ptr> AMap; However, when I compile, I get the following error: > error: type/value mismatch at argument 1 in template parameter list >

Why does enable_shared_from_this lack direct access to the embedded weak_ptr?

徘徊边缘 提交于 2019-12-06 14:22:13
I want to use boost signals2 with automatic connection management in a multithreaded application. My class inherits from enable_shared_from_this<> and i want to connect a member method from within another member method. The connection might be rebuilt frequently so my code should be as fast as possible (despite from the boost signals2 performance itself): typedef boost::signals2::signal<void ()> signal_type; struct Cat : public enable_shared_from_this<Cat> { void meow (); void connect (signal_type& s) { // can't write this s.connect (signal_type::slot_type (&Cat::meow, this, _1).track (weak

How to make all copies of a shared_ptr equal to another shared_ptr?

强颜欢笑 提交于 2019-12-06 11:19:08
I cannot figure this out.. Looks like I'm missing something simple? What do I put in MakePointToSameValue so that at point (1) both b.ptr and c.ptr point to the same as a.ptr in other words, a.ptr.get() == b.ptr.get() == c.ptr.get() the value originally pointed to by b.ptr and c.ptr gets deleted ? struct Test { public: Test( int val ) : ptr( std::make_shared< int >( val ) ) { } void MakePointToSameValue( Test& other ) { //what do I put here? //other.ptr = this->ptr; //doesn't do it } private: std::shared_ptr< int > ptr; }; Test a( 0 ); Test b( 5 ); Test c( b ); b.MakePointToSameValue( a ); //

Using mem_fun_ref with boost::shared_ptr

爱⌒轻易说出口 提交于 2019-12-06 10:37:58
Following the advice of this page , I'm trying to get shared_ptr to call IUnknown::Release() instead of delete: IDirectDrawSurface* dds; ... //Allocate dds return shared_ptr<IDirectDrawSurface>(dds, mem_fun_ref(&IUnknown::Release)); error C2784: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Result (__thiscall _Ty::* )(_Arg) const' from 'ULONG (__cdecl IUnknown::* )(void)' error C2784: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void) const)' : could not