shared-ptr

New to c++11 features, proper use of shared_ptr?

﹥>﹥吖頭↗ 提交于 2019-12-23 18:08:48
问题 So my understanding is that a shared_ptr automatically deallocates from memory when the last remaining owner of the object is destroyed or reassigned, (Seems too good to be true?) and it's useful when many instances may be sharing the same object. Correct? So in my case, I'm making a 2d tiled world, so I'm drawing many of the same texture to the screen. I have std::map<int, shared_ptr<Tile>> Tiledb; to store all the tiles. The idea is to only load the texture in once and then i can render it

Debuggable replacement for make_shared()

﹥>﹥吖頭↗ 提交于 2019-12-23 18:05:36
问题 Using gcc 4.6.2, make_shared() gives a useless backtrace (apparently due to some rethrow) if a constructor throws an exception. I'm using make_shared() to save a bit of typing, but this is show stopper. I've created a substitute make_shrd() that allows a normal backtrace. I'm using gdb 7.3.1. I'm worried that: The bad backtrace under make_shared() is somehow my own fault My substitute make_shrd() will cause me subtle problems. Here's a demo: #include <memory> #include <stdexcept> using

Delete an object with a protected destructor

风流意气都作罢 提交于 2019-12-23 17:15:15
问题 I have to write a shared pointer for class, and among many other things that it has to do is make sure it can delete the object that it is pointing to. How can I code a solution that will work with an object that has a protected destructor? Additionally, if the object was created using placement new, I should not be calling delete on the the object, as that space may still be in use (will the delete call even work?). How can I detect such cases? The relevant bits of the spec: void reset();

how to use postThreadMessage to pass a struct

本小妞迷上赌 提交于 2019-12-23 13:09:09
问题 I want to use windows's message queue facilities to send a struct to another thread. But I find out that the postthreadmessage function only provide two integer parameters, lparam and wparam for me to pass arguments.So I decide to put the address of the struct in lparam. Is this the correct way windows use to pass struct? And I intend to use boost::shared_ptr to hold the address of struct in both the receiver thread and sender thread. I doubt that when the two shared_ptrs goes out of scope,

Qt raw vs std::shared_ptr

穿精又带淫゛_ 提交于 2019-12-23 12:13:10
问题 I noticed that when substituting raw pointers with shared_ptr in QT, my code does not work anymore. For example, if instead of QTreeWidgetItem* vItem(new QTreeWidgetItem(ItemTitle)); I use std::shared_ptr<QTreeWidgetItem> vItem(new QTreeWidgetItem(ItemTitle)); then, either the program crashes or nothing is done (even if I use the .get() function to get the raw pointer from the shared one later in my code). Does anybody knows what could be the cause? 回答1: Using shared pointer with Qt model

Can I create a boost::shared_ptr to a local variable?

余生长醉 提交于 2019-12-23 10:08:56
问题 I have some methods that take a reference to a given object, and some are taking boost::shared_ptr . So far in my test method I created a shared_ptr pointing to one of these objects and pass *ptr to the methods expecting a reference. Is it possible to do it the other way round, e.g. create a local object on the stack, and then create a shared pointer to it in a safe way, to arrive at the straightforward alternative to &obj operator with traditional pointers? 回答1: #include <boost/shared_ptr

Why is there no [] operator for std::shared_ptr? [duplicate]

我只是一个虾纸丫 提交于 2019-12-23 09:29:11
问题 This question already has an answer here : Why isn't there a std::shared_ptr<T[]> specialisation? (1 answer) Closed 4 years ago . I wonder what the rationale is behind the fact, that std::shared_ptr does not define the [] operator for arrays. In particular why does std::unique_ptr feature this operator but not std::shared_ptr ? 回答1: std::unique_ptr only defines operator[] in a specialization for arrays: std::unique_ptr<T[]> . For non-array pointers, the operator[] doesn't make much sense

How does a shared_ptr store deleter?

有些话、适合烂在心里 提交于 2019-12-23 07:46:13
问题 I can't understand how a shared_ptr can store the deleter that I gave to it. Initially, using a shared_ptr<int> , i thought it might use an std::function<void(int*)> , but i can give, as a deleter, any kind of function (or callable objects), as long as the first parameter is a int* . How can shared_ptr do this? I'm sorry if this is a silly question, I'm new to C++, forgive me! Edit: The question is: how can I do something like that? What should I use? Any example? Or it is a very advanced

How does a shared_ptr store deleter?

∥☆過路亽.° 提交于 2019-12-23 07:45:59
问题 I can't understand how a shared_ptr can store the deleter that I gave to it. Initially, using a shared_ptr<int> , i thought it might use an std::function<void(int*)> , but i can give, as a deleter, any kind of function (or callable objects), as long as the first parameter is a int* . How can shared_ptr do this? I'm sorry if this is a silly question, I'm new to C++, forgive me! Edit: The question is: how can I do something like that? What should I use? Any example? Or it is a very advanced

Assigning existing values to smart-ptrs?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 07:30:09
问题 I am just learning about smart pointers, and I am having trouble assigning a pre-existing location of a variable to the standard library's shared pointer. For example, lets say you have an int x, which you do not know the value of. With normal pointers, I just did int* ptr; ptr = &x; I tried both that with shared pointers, and std::tr1::shared_ptr<int> ptr; ptr = std::make_shared<int> (&x) So i'm fairly lost as to how to do it. 回答1: You wouldn't (usually) make a smart pointer point to an