shared-ptr

Is there a way to increase the efficiency of shared_ptr by storing the reference count inside the controlled object?

邮差的信 提交于 2019-12-20 02:56:28
问题 This is becoming a common pattern in my code, for when I need to manage an object that needs to be noncopyable because either A. it is "heavy" or B. it is an operating system resource, such as a critical section: class Resource; class Implementation : public boost::noncopyable { friend class Resource; HANDLE someData; Implementation(HANDLE input) : someData(input) {}; void SomeMethodThatActsOnHandle() { //Do stuff }; public: ~Implementation() { FreeHandle(someData) }; }; class Resource {

ntdll.dll [Frames below may be incorrect /missing, no symbols loaded for ntdll.dll]

扶醉桌前 提交于 2019-12-20 02:17:04
问题 since a few weeks now I have run into this situation and it very upsetting, my program debugs very well, no errors, when I run the program it does the initial part of the job, after a few seconds It terminates to a 'break continue' option, and in the call stack when I have a look, it turns out that its an error from ntdll.dll and msvcr100d.dll it seems like a windows related SDK problem, it seems like a bad pointer too. I have somehow no control over this. I installed windows SDK to 7.0 but

C++0x Error: overloading a function with std::shared_ptr to const argument is ambiguous

落花浮王杯 提交于 2019-12-19 19:54:06
问题 Suppose I have two unrelated classes A and B . I also have a class Bla that uses boost::shared_ptr like this: class Bla { public: void foo(boost::shared_ptr<const A>); void foo(boost::shared_ptr<const B>); } Notice the const . That's the important part which the original version of this question lacked. This compiles, and the following code works: Bla bla; boost::shared_ptr<A> a; bla.foo(a); However, if I switch from using boost::shared_ptr to using std::shared_ptr in the above examples, I

C++: Replace raw pointers with shared and weak ptr

怎甘沉沦 提交于 2019-12-19 09:12:24
问题 I'm facing a design issue in my program. I have to manage Nodes object which are part of a root ChainDescriptor. Basically it looks like the following: class ChainDescriptor { public: ~ChainDescriptor() { //delete the nodes in nodes... } void addNode(Node *); Node * getNode(); const std::list<Node *>& getNodes() const; std::list<Node *> m_nodes; }; class Node { public: Node(Node *parent); void addChild(Node *node); Node * getChild(const std::string& nodeName); private: Node * m_parent; std:

C++: Replace raw pointers with shared and weak ptr

╄→гoц情女王★ 提交于 2019-12-19 09:11:08
问题 I'm facing a design issue in my program. I have to manage Nodes object which are part of a root ChainDescriptor. Basically it looks like the following: class ChainDescriptor { public: ~ChainDescriptor() { //delete the nodes in nodes... } void addNode(Node *); Node * getNode(); const std::list<Node *>& getNodes() const; std::list<Node *> m_nodes; }; class Node { public: Node(Node *parent); void addChild(Node *node); Node * getChild(const std::string& nodeName); private: Node * m_parent; std:

Set shared_ptr to point existing object

拟墨画扇 提交于 2019-12-19 07:09:28
问题 For the code below, I would like to know how to set std::shared_ptr to point the given objects in the two member functions. The Vector3 object which is allocated in the main function is not going to be deleted until the end of the program. #include <memory> #include <vector> using std::vector; using std::shared_ptr; class Vector3 { // ... }; class Face { vector < shared_ptr <Vector3> > vtx; public: void addVtx (const Vector3& vt) { // vtx.push_back (); <-- how to push_back ? } void setVtx

Implicit conversion from int to shared_ptr

社会主义新天地 提交于 2019-12-19 07:05:44
问题 Consider the code below: #include <iostream> #include <memory> void f(std::shared_ptr<int> sp) {} template <typename FuncType, typename PtrType> auto call_f(FuncType f, PtrType p) -> decltype(f(p)) { return f(p); } int main() { f(0); // doesn't work for any other int != 0, thanks @Rupesh // call_f(f, 0); // error, cannot convert int to shared_ptr } In the first line in main() , the integer 0 is converted to a std::shared_ptr<int> and the call f(0) succeeds without any problem. However, using

Implicit conversion from int to shared_ptr

二次信任 提交于 2019-12-19 07:05:42
问题 Consider the code below: #include <iostream> #include <memory> void f(std::shared_ptr<int> sp) {} template <typename FuncType, typename PtrType> auto call_f(FuncType f, PtrType p) -> decltype(f(p)) { return f(p); } int main() { f(0); // doesn't work for any other int != 0, thanks @Rupesh // call_f(f, 0); // error, cannot convert int to shared_ptr } In the first line in main() , the integer 0 is converted to a std::shared_ptr<int> and the call f(0) succeeds without any problem. However, using

shared_ptr vs CComPtr

蓝咒 提交于 2019-12-19 03:56:19
问题 I'm somewhat used to the concept of refcounting through COM and I'm somewhat new to shared_ptr. There are several nice properties with CComPtr that I don't find in shared_ptr, and I'm wondering what are the pattern that prevent missuse of shared_ptr. The AddRef/Release pattern guarantees there is only one refcount per object (the refcount is stored on the object itself), so it's safe, when you have a random pointer to create a CComPtr around it. On the other hand, shared_ptr has a separate

How does a weak_ptr know that the shared resources has expired?

流过昼夜 提交于 2019-12-18 19:28:34
问题 Considering the following code: #include <memory> #include <iostream> using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr<MySharedStruct> weakPtr) { if (shared_ptr<MySharedStruct> sp = weakPtr.lock()) { cout << "Value of i = " << sp->i << endl; } else { cout << "Resource has expired"; } } int main() { shared_ptr<MySharedStruct> sharedPtr(new MySharedStruct() ); sharedPtr->i = 5; weak_ptr<MySharedStruct> weakPtr; weakPtr = sharedPtr; print_value_of_i(weakPtr);