smart-pointers

Passing handle to C++ classes across a C API boundary

寵の児 提交于 2019-12-13 03:07:01
问题 I am writing a library in C++, but want it to have a C API, which should also be thread safe. One thing that the API needs to do is to pass back and forth handles (e.g. a structure containing a reference or pointer) of objects created within the library. These objects need to be destroyed at some point, so any handles to such an object that were still in existence would them become invalid. EDIT: We cannot assume that each handle is only used within a single client thread. In particular I

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? */} };

Proper Implementation of Copy Constructor and Equals Operator on a class with smart pointers

醉酒当歌 提交于 2019-12-13 02:28:58
问题 Suppose I want to implement a class which is copyable, so I can implement the copy constructor and assignment operator. However, what is the correct implementation and handling of unique and shared pointer variables? See this contrived example which has both types of pointers: Header File #include <memory> using std::unique_ptr; using std::shared_ptr; class Copyable { private: unique_ptr<int> uniquePointer; shared_ptr<int> sharedPointer; public: Copyable(); Copyable(int value); Copyable(const

How to std::bind a smart pointer return method?

余生颓废 提交于 2019-12-13 01:49:19
问题 So I have this method inside my Bar class: std::shared_ptr<sf::Sprite> Bar::getStuff() const { //... } And I have my callback typedef: typedef std::function<void()> Callback; void Foo::registerCallback(const Callback& callback) { //... } And now I want to use std::bind on this method, like: Foo foo; Bar bar; //I create an instance of an Bar, called bar. foo.registerCallback(std::bind(&Bar::getStuff, std::ref(bar))); //<--- ERROR!!!! ERROR: error C2562: 'std::_Callable_obj<std::_Bind<true,std:

finding who creates object via smart pointer

霸气de小男生 提交于 2019-12-13 00:37:07
问题 I posted few days back regarding memory leaks with smart pointers. Now I am able to find out which objects are leaking memory but I am not able to figure it out from where they are leaking memory. I am using the same code as mentioned here Detecting memory leak in reference counted objects I have read lot of comments but none of them explains properly. I tried using macros FILE and LINE , both of them prints file and line of refmanager class . Is there a good way to debug this issue.Please

smart pointer array deletor

喜你入骨 提交于 2019-12-12 19:15:35
问题 Up to now i've allocated a buffer for some data processing and deleted it afterwards. Since the code got bigger and caught exceptions at some points can occur, i thought about making it safer with a std::unique_ptr and came up with those solutions: unique_ptr<char, void (*)(void *)> p1( (char*)operator new(bufSize), operator delete); unique_ptr<char[], void (*)(void *) > p2( (char*)operator new(bufSize), operator delete); memcpy(&((p1.get())[0]), "xyz", 3); memcpy(&(p2[0]), "xyz", 3); char x1

Why does Arc::try_unwrap() cause a panic?

帅比萌擦擦* 提交于 2019-12-12 19:08:57
问题 I'm writing a simple chat server which broadcasts messages to all the clients connected. The code might look terrible, since I'm a beginner. Peers are not used anywhere yet, since I want to pass it to handle_client function as well, so when data will be available in stream and read successfully, I want to broadcast it over all the clients connected. I understand this is not a good approach, I'm just trying to understand how can I do things like this in general. use std::io::BufRead; use std:

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,

How do I pass Rc<RefCell<Box<MyStruct>>> to a function accepting Rc<RefCell<Box<dyn MyTrait>>>?

梦想的初衷 提交于 2019-12-12 11:26:08
问题 I have originally asked this question here, but it was marked as duplicate, although it duplicates only a part of it in my opinion, so I have created a more specific one: Consider the following code: use std::rc::Rc; trait MyTrait { fn trait_func(&self); } struct MyStruct1; impl MyStruct1 { fn my_fn(&self) { // do something } } impl MyTrait for MyStruct1 { fn trait_func(&self) { // do something } } fn my_trait_fn(t: Rc<dyn MyTrait>) { t.trait_func(); } fn main() { let my_str: Rc<MyStruct1> =

Simple reference counting: smart pointers

社会主义新天地 提交于 2019-12-12 10:49:42
问题 I would like to implement a simple reference counting using smart pointers. The variable pointer represents pointer to stored object, reference_count represents total count of copies of the object. if we initialize an object using NULL: reference_count = -1 else reference_count = 1 copy ctor and operator = increment reference_count destructor decrement reference_count and if there is no other reference to pointed object performs its deletion. Here is my code: #ifndef smart_pointer_H #define