smart-pointers

.Access violation reading location

ε祈祈猫儿з 提交于 2019-12-06 16:03:09
问题 I've met a really strange problem. The code is as follow: ::boost::shared_ptr<CQImageFileInfo> pInfo=CQUserViewDataManager::GetInstance()->GetImageFileInfo(nIndex); Image* pImage=pInfo->m_pThumbnail; if(pImage==NULL) pImage=m_pStretchedDefaultThumbImage; else { // int sourceWidth = pInfo->GetWidth(); int sourceHeight = pInfo->GetHeight(); int destX = 0, destY = 0; float nPercent = 0; float nPercentW = ((float)GetThumbImageWidth()/(float)sourceWidth);; float nPercentH = ((float

boost::shared_ptr cycle break with weak_ptr

◇◆丶佛笑我妖孽 提交于 2019-12-06 13:36:20
I am currently in a situation like: struct A { shared_ptr<B> b; }; struct B { shared_ptr<A> a; }; //... shared_ptr<A> a(new A()); shared_ptr<B> b(new B()); a->b(b); b->a(a); I know this won't work, because the references would continue to point to each other. I've also been told that weak_ptr solves this issue. However, weak_ptr has no get or -> overload. I've heard mentions of 'use lock() ', but can anyone give code examples of how to do this correctly? Come on now. http://boost.org/doc/libs/1_42_0/libs/smart_ptr/weak_ptr.htm ^^^^^ EXAMPLE IS RIGHT THERE ^^^^^^ DAMN! I think the bigger issue

Are shared_ptr on static objects good?

自作多情 提交于 2019-12-06 11:22:48
问题 I'm wondering whether smart pointers on static objects are reasonable. For example let's say I have some static resource and want to pass a reference to that static resource to some other objects which need these resource to work with. One way would be to use the RAW-Pointers pointing to that resource. But now I'm wondering whether smart pointers (shared_ptr) are the better way and if so, how to do it correctly. (should the smart pointer be static as well?). Background of the question: if no

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 ); //

Weak resources and factory design pattern

ⅰ亾dé卋堺 提交于 2019-12-06 09:56:07
Thinking of a way how to best approach the following design (pseudo C++ code): class Display { public: Bitmap GetBitmap(uint16 width, uint16 height); private: // A list of active bitmaps std::set<Bitmap> bitmaps; } ... where Display is a factory (and sole owner) for Bitmap objects. Display also has to track all the allocated bitmaps within a std::set container and may choose to invalidate/delete them at any time (like in a fullscreen switch). So essentially a Bitmap is a weak resource handle and whoever holds the handle will have to check if it is valid before using it. My theoretical solution

Implementing Containers using Smart Pointers

十年热恋 提交于 2019-12-06 04:39:01
问题 Ok, so everyone knows that raw pointers should be avoided like the plague and to prefer smart pointers, but does this advice apply when implementing a container? This is what I am trying to accomplish: template<typename T> class AVLTreeNode { public: T data; unique_ptr<AVLTreeNode<T>> left, right; int height; } Unique_ptr can make container functions more cumbersome to write because I can't have multiple raw pointers temporarily pointing to the same object in a way that is elegant. For

Convert container of pointers to smart pointers?

和自甴很熟 提交于 2019-12-06 02:46:40
Is there a concise, generic way to convert a std container (such as vector ) of regular/dumb pointers: vector< T* > to, for instance, boost::shared_ptr ?: vector< boost::shared_ptr<T> > I thought I could pull it off using vector 's range constructor: vector< T* > vec_a; ... vector< boost::shared_ptr<T> > vec_b( vec_a.begin(), vec_a.end() ); but that refused to compile (Visual Studio 2008). EDIT: Test code: void test() { vector< int* > vec_a; vector< boost::shared_ptr<int> > vec_b( vec_a.begin(), vec_a.end() ); } Compilation errors: 1>c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC

Smart pointers with addrinfo struct

北战南征 提交于 2019-12-06 01:57:05
I need to deal with two struct addrinfo pointers. Since I'm coding in C++(11), I've to make my code exception-safe. Indeed, my costructors may throw a runtime_error . When you don't need that kind of struct anymore, you should call freeaddrinfo in order to free the list inside the struct. Please consider the following code: #include <memory> #include <netdb.h> class SomeOtherClass { public: SomeOtherClass() : hints(new addrinfo), result(new addrinfo) { /*stuff*/ } ~SomeOtherClass() { freeaddrinfo(result.get()); } // bad things will happen private: std::unique_ptr<addrinfo> hints, result; };

Why doesn't std::shared_ptr use reference linking?

佐手、 提交于 2019-12-06 01:47:40
std::shared_ptr needs to allocate a control block on the heap which holds the reference count. There was another approach I learnt from http://ootips.org/yonat/4dev/smart-pointers.html which keeps all the references in a doubly linked list. It doesn't need additional allocations nor a counter but the reference object itself is larger. Is there a benchmark or any clear reason showing one implementation is better than the others? The standard does in theory allow a linked list to be used, but because copying a shared_ptr must be threadsafe it would be harder to implement that with a linked list.

virtual constructor idiom with smart pointers

会有一股神秘感。 提交于 2019-12-06 01:35:25
问题 I've a hierarchy of polymorphic classes, say a Shape abstract base class together with its derived classes, e.g. Rectangle , Circle , etc. Following the Virtual Constructor Idiom, I was wondering why we need that the return types of the virtual constructor functions in the derived classes should return the same type as in its parent class when using smart pointers? For example, see the code below, where the clone() and create() member functions need to return smart_pointers to the Shape class