smart-pointers

C++ leaks in case of exception even by using smart pointers

心已入冬 提交于 2019-12-10 12:44:23
问题 I am new to the smart pointers world. I've done my reading and all of them stated that smart pointers will avoid leaking memory even when the program will exit after encountering an exception. I wrote down a simple program to try this out, but Valgrind is telling me my program is leaking memory (three allocs and only one free). This is the source code: #include <iostream> #include <memory> using namespace std; int main() { auto_ptr<int> ptr_int(new int(5)); throw std::bad_alloc(); cout <<

Weak resources and factory design pattern

感情迁移 提交于 2019-12-10 11:33:47
问题 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

smart pointers for modelling a general tree structure & its iterators

你说的曾经没有我的故事 提交于 2019-12-10 10:52:09
问题 I am modelling a general tree structure by having a class for each node with pointers to parent, first child and first sibling, plus a pointer to last sibling (not needed, but useful). To this, I am adding some extra data atd. My current implementation is: class TreeNode { typedef boost::shared_ptr<TreeNode> Ptr; typedef boost::weak_ptr<TreeNode> WPtr; WPtr p2parent; ///< pointer to the parent node (NULL in the root) Ptr p2sibling; ///< pointer to the first sibling (or NULL) Ptr p2child; ///<

QCache and QSharedPointer

 ̄綄美尐妖づ 提交于 2019-12-10 10:25:58
问题 The problem is, that I have a QVector of QSharedPointer and I want to put part of them to my QCache . How can I insert a shared pointer to QCache ? What happens if I set the pointer to my cache but destroy the QVector and the shared pointer in it? Will the memory be released and my cache points to nowhere? 回答1: It is a bug if you put just a pointer to your item to QChache and at the same time such pointer is managed by QSharedPointer . The item object can be destroyed by QSharedPointer

How to remove smart pointers from a cache when there are no more references?

两盒软妹~` 提交于 2019-12-10 04:03:32
问题 I've been trying to use smart pointers to upgrade an existing app, and I'm trying to overcome a puzzle. In my app I have a cache of objects, for example lets call them books. Now this cache of books are requested by ID and if they're in the cache they are returned, if not the object is requested from an external system (slow operation) and added to the cache. Once in the cache many windows can be opened in the app, each of these windows can take a reference to the book. In the previous

intrusive_ptr: Why isn't a common base class provided?

廉价感情. 提交于 2019-12-10 03:15:07
问题 boost::intrusive_ptr requires intrusive_ptr_add_ref and intrusive_ptr_release to be defined. Why isn't a base class provided which will do this? There is an example here: http://lists.boost.org/Archives/boost/2004/06/66957.php, but the poster says "I don't necessarily think this is a good idea". Why not? Update: I don't think the fact that this class could be misused with Multiple Inheritance is reason enough. Any class which derives from multiple base classes with their own reference count

auto_ptr or shared_ptr equivalent in managed C++/CLI classes

送分小仙女□ 提交于 2019-12-10 02:27:00
问题 In C++/CLI , you can use native types in a managed class by it is not allowed to hold a member of a native class in a managed class : you need to use pointers in that case. Here is an example : class NativeClass { .... }; public ref class ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeClass; // OK auto_ptr<NativeClass> mNativeClass; //Not allowed ! boost::shared_ptr<NativeClass> mNativeClass; //Not allowed ! }; Does anyone know of an equivalent of

Why is unique_ptr operator-> not const-overloaded?

时间秒杀一切 提交于 2019-12-10 02:11:51
问题 std::unique_ptr::operator-> has the signature pointer operator->() const noexcept; So operator-> is const but returns a mutable pointer. This allows for code like: void myConstMemberFunction() const { myUniquePtrMember->nonConstFunction(); } Why does the standard allow this, and what is the best way to prevent usage as presented above? 回答1: Think about it like a normal pointer: int * const i; is a const pointer to a non- const int . You can change the int , but not the pointer. int const * i;

Boost.Pointer Container made obsolete by std::unique_ptr in C++11/14?

倖福魔咒の 提交于 2019-12-09 16:26:04
问题 Does std::unique_ptr make Boost.Pointer Container library obsolete in C++11/14? In C++98/03 there isn't move semantics, and a smart pointer like shared_ptr has reference-counting related overhead (both for the ref counting block, and the interlocked increment/decrements) if compared to raw pointers. So something like std::vector<shared_ptr<T>> has overhead if compared to std::vector<T*> . But is std::vector<std::unqiue_ptr<T>> just as efficient as std::vector<T*> (no reference counting

Feasibility of automatic cycle breaker for `std::shared_ptr`

旧巷老猫 提交于 2019-12-09 09:21:29
问题 C++11 introduced reference-counted smart pointers, std::shared_ptr . Being reference counted, these pointers are unable to automatically reclaim cyclic data structures. However, automatic collection of reference cycles was shown to be possible, for example by Python and PHP. To distinguish this technique from garbage collection, the rest of the question will refer to it as cycle breaking . Given that there seem to be no proposals to add equivalent functionality to C++, is there a fundamental