shared-ptr

shared_ptr with incomplete types from library

大城市里の小女人 提交于 2019-12-14 01:41:48
问题 My problem is straightforward: I am using SDL to create a simple simulation and I want to store instances of TTF_Font type in smart pointers (shared_ptr), but I keep getting this error: "invalid application of ‘sizeof’ to incomplete type '_TTF_Font'" Is there any way to use smart pointers with incomplete types from external libraries without incorporating their source code into my program? EDIT: TTF_Font is declared as typedef struct _TTF_Font TTF_Font; _TTF_Font is in turn defined in

Shared ownership of IDisposable objects in C#

≡放荡痞女 提交于 2019-12-13 12:23:06
问题 Is there any classes in C# which provide shared ownership of IDisposable objects? Something like shared_ptr in c++? And if not, what are best practices here? UPDATE I'm writing a c++/cli wrapper over native lib. And I need release native resources ( MAPI COM interfaces for example, so I need determenistic resource releasing ). Native part: //Message.h class Message { ... }; //MessageSet.h class MessageSet { ... class iterator : public std::iterator<std::forward_iterator_tag, Message*> { ...

Forward declarations and shared_ptr

妖精的绣舞 提交于 2019-12-13 11:39:42
问题 I'm trying to refactor my code so that I use forward declarations instead of including lots of headers. I'm new to this and have a question regarding boost::shared_ptr. Say I have the following interface: #ifndef I_STARTER_H_ #define I_STARTER_H_ #include <boost/shared_ptr.hpp> class IStarter { public: virtual ~IStarter() {}; virtual operator()() = 0; }; typedef boost::shared_ptr<IStarter> IStarterPtr; #endif I then have a function in another class which takes an IStarterPtr object as

Update a smart pointer using a reference

人盡茶涼 提交于 2019-12-13 08:45:47
问题 I would like to update a smart pointer from a reference. shared_ptr<My_Toy> my_toy_ptr; // Something... void update(shared_ptr<My_Toy> my_toy_ptr, My_Toy& toy){ my_toy_ptr = &toy; } ...but his code generates an error. How can I do this operation? 回答1: Don't pass the address of a stack allocated object to a std::shared_ptr . toy will be destructed at the end of its scope and the std::shared_ptr will attempt to delete something that was not new d. The address held by a std::shared_ptr must be

Multiple inside-of-class typedef of shared_ptr

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 04:46:13
问题 I can currently initialize the following class MyTest template<class T> class MyTest { public: typedef std::shared_ptr<MyTest> Ptr; MyTest( Ptr _nextTest = NULL ) : m_nextTest( _nextTest ) { } ~MyTest() { } private: Ptr m_nextTest; }; like so MyTest<int>::Ptr my( new MyTest<int>() ); What I would now also like to be capable of is having a MyTest of a different type as an argument for the constructor as in MyTest<int>::Ptr my( new MyTest<float>() ); But this obviously brakes (*1) as my typedef

why syslog sink sends same records on all the sinks when a sink is member of class?

為{幸葍}努か 提交于 2019-12-13 03:32:22
问题 I have written following code to send logs to remote syslog( rsyslog on remote unix machine ) from my windows machine using boost-log . I am using syslog_backend . #include <boost/config.hpp> #if !defined(BOOST_WINDOWS) #define BOOST_LOG_USE_NATIVE_SYSLOG #endif #include <string> #include <iostream> #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/log/common.hpp> #include <boost/log/expressions.hpp> #include <boost/log/attributes.hpp> #include <boost/log/sinks/sync_frontend.hpp>

C++ safe idiom to call a member function of a class through a shared_ptr class member

耗尽温柔 提交于 2019-12-13 02:55:30
问题 Problem description In designing an observer pattern for my code, I encountered the following task: I have a class Observer which contains a variable std::shared_ptr<Receiver> and I want to use a weak_ptr<Receiver> to this shared-pointer to safely call a function update() in Observer (for a more detailed motivation including some profiling measurements, see the EDIT below). Here is an example code: struct Receiver { void call_update_in_observer() { /* how to implement this function? */} };

Understanding C++ std::shared_ptr when used with temporary objects

自古美人都是妖i 提交于 2019-12-12 17:19:06
问题 I am trying to understand the use of a shared_ptr p when it is used in the construction of an unnamed shared_ptr and the effects this has on p . I was toying with my own examples and wrote the following piece of code: shared_ptr<int> p(new int(42)); cout << p.use_count() << '\n'; { cout << p.use_count() << '\n'; shared_ptr<int>(p); cout << p.use_count() << '\n'; } cout << p.use_count() << '\n'; Output: 1 1 0 1 Is it correct that line 5, uses p to create a temp. shared_ptr (i.e an unnamed

Delete std::shared_ptr without destroying the managed object?

一曲冷凌霜 提交于 2019-12-12 13:45:50
问题 I'm in the following scenario: struct container { data* ptr; }; void someFunc(container* receiver /* wants to be filled */) { auto myData = createData(); // returns shared_ptr<data> receiver->ptr = myData.get(); } The function that generates that data, and the object that receives it, are part of two different libraries, which I can't modify the source code of. I have to work with these data types, there's nothing I can do about it. So I have to implement a function that acquires some data,

pimpl: Avoiding pointer to pointer with pimpl

烈酒焚心 提交于 2019-12-12 13:07:44
问题 In this question I asked "pimpl: shared_ptr or unique_ptr" I've been convinced that the proper usage of the pimpl idiom is to use a unique_ptr , not a shared_ptr . It should act to the user as if there is no pointer at all, whereas quite clearly the shared_ptr introduces aliasing upon copying, which definitely acts like a pointer. So, lets say a user wants to create a shared_ptr to my pimpl object (say if they want actually want multiple aliases to it). For example: shared_ptr<my_pimpl> p(new