shared-ptr

how to delete a shared pointer value in vector

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-05 04:35:13
问题 I have type defined a shared pointer as : typedef shared_ptr<Myclass> Sptr; then a vector : vector<Sptr> vectr ; now I have stored several shared pointers in a vector, each is pointing to a dynamically allocated memory. now I want to delete particular element(child) in a vector(children.begin() to children.end()). ItemList::iterator it = find(children.begin(), children.end(), child); if (it != children.end()) { //delete it; it = children.erase(it); } Now children.erase(it), will this delete

boost::shared_ptr boost::mutex and copy constructor

送分小仙女□ 提交于 2020-01-04 03:48:50
问题 I need to protect the access to a data structure in my class. As I can't have mutex (because I can't copy it) I am considering to have shared_ptr and keep the mutex there. Here is a sample code of my idea: class Sample { typedef boost::lock_guard<boost::mutex> AcquireLock; boost::shared_ptr<boost::mutex> mutt; public: Sample() : mutt(new boost::mutex) {} void Method() { AcquireLock lock(*mutt); //do some work here } }; I've got the following questions: Is it a bad practice to use the mutex

Shared pointers and queues in FreeRTOS

老子叫甜甜 提交于 2020-01-03 12:05:22
问题 A C++ wapper around a FreeRTOS queue can be simplified into something like this: template<typename T> class Queue<T> { public: bool push(const T& item) { return xQueueSendToBack(handle, &item, 0) == pdTRUE; } bool pop(T& target) { return xQueueReceive(handle, &target, 0) == pdTRUE; } private: QueueHandle_t handle; } The documentation of xQueueSendToBack states: The item is queued by copy, not by reference. Unfortunately, it is literally by copy, because it all ends in a memcpy , which makes

c++ how to assert that all std::shared_ptr in a vector are referring to something

天涯浪子 提交于 2020-01-03 09:29:10
问题 When I have a function receiving a (smart) pointer that is supposed to refer something, I always start as follows: class Foo; void doSomething(const std::shared_ptr<Foo>& pFoo) { assert(pFoo); // ... } Now I am looking for a similar assert condition for a vector (or other container) of (smart) pointers. The best I could come up with is: void doSomething(const std::vector<std::shared_ptr<Foo> >& pFoos) { assert(std::all_of(pFoos.begin(), pFoos.end(), [](const std::shared_ptr<Foo>& pFoo) {

Using shared_ptr and glutInit causes segmentation fault

北城以北 提交于 2020-01-03 07:17:34
问题 Having asked this before I tried out a lot of things and found out that the problem has to do with glutInit. Take the following code examples: main.cpp #include <iostream> #include <memory> #include<GL/glut.h> using namespace std; int main(int argcp, char **argv) { shared_ptr<double> abc; glutInit(&argcp,argv); cout<<"Hello!"<<endl; return 0; } compiled with: g++ -std=c++11 -g -Wall -o appx main.cpp -lGL -lGLU -lglut cause the executable to instantly crash (no "Hello!" output) with segfault

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

本秂侑毒 提交于 2020-01-02 18:34:14
问题 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:

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

不问归期 提交于 2020-01-02 18:34:09
问题 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:

Shared_Ptr of socket creation - what is wrong?

你离开我真会死。 提交于 2020-01-02 15:31:48
问题 So I try: boost::shared_ptr<tcp::socket> socket = boost::make_shared<tcp::socket>(io_service); As described here. But It bring me an error: Compiler tells me that it can not turn ( error C2664: boost::asio::basic_stream_socket<Protocol>::basic_stream_socket( boost::asio::io_­service &)) 'boost::asio::io_service *const ' into 'boost::asio::io_service &' \include\boost\smart_ptr\make_shared.hpp What shall I do? 回答1: You need to pass the io_service as a reference when using make_shared . boost:

shared_ptr null pointer and assignment

爷,独闯天下 提交于 2020-01-02 05:54:22
问题 I want to use shared_ptr just like I'd use an actual pointer. I wanted to be able to do things like shared_ptr<int> a; a = new int(5); a = 0; shared_ptr<int> foo() return 0; but it is not implemented by default. I changed the source code of the shared_ptr of the boost library by adding template<class Y> void operator=( int i ) { reset(i); } template<class Y> void reset(int i) { this_type(i).swap(*this); } template<class Y> void operator=( Y * p ) { reset(p); } shared_ptr(int i): px(0), pn() {

Customising std::shared_ptr or boost::shared_ptr to throw an exception on NULL dereference

谁说我不能喝 提交于 2020-01-02 03:50:49
问题 I have a few projects that use boost::shared_ptr or std::shared_ptr extensively (I can convert to either implementation soon enough, if there is a good answer to this question for one, but not the other). The Boost implementation uses Boost.Assert to avoid returning in the case of encountering an empty (NULL) pointer in operator* or operator-> at runtime; while the libc++ implementation seems to lack any check. While of course the validity of a shared_ptr should be checked before use, a large