Vector of shared pointers , memory problems after clearing the vector

you have two copies of shared_ptr<A> in this case, one is the sharedptr variable and the other as an element in the vector. do this instead test.push_back(std::move(sharedptr)); note now the original sharedptr has it’s internal moved and no longer usable. The other thing is don’t do anything at all, this is a perfectly valid usage … Read more

C++ using scoped_ptr as a member variable

scoped_ptr is very good for this purpose. But one has to understand its semantics. You can group smart pointers using two major properties: Copyable: A smart pointer can be copied: The copy and the original share ownership. Movable: A smart pointer can be moved: The move-result will have ownership, the original won’t own anymore. That’s … Read more

Return Type Covariance with Smart Pointers

You can’t do it directly, but there are a couple of ways to simulate it, with the help of the Non-Virtual Interface idiom. Use covariance on raw pointers, and then wrap them struct Base { private: virtual Base* doClone() const { … } public: shared_ptr<Base> Clone() const { return shared_ptr<Base>(doClone()); } virtual ~Base(){} }; struct … Read more

C++11 Smart Pointer Semantics

Even in C++11, raw pointers are still perfectly valid as non-owning references to objects. In your case, you’re saying “Let’s assume the textures are guaranteed to outlive their pointers.” Which means you’re perfectly safe to use raw pointers to the textures in the game objects. Inside the texture manager, store the textures either automatically (in … Read more

intrusive_ptr in c++11

Does c++11 have something equivalent to boost::intrusive_ptr? No. It does have std::make_shared which means std::shared_ptr is almost (see note below) as efficient as an intrusive smart pointer, because the reference counts will be stored adjacent in memory to the object itself, improving locality of reference and cache usage. It also provides std::enable_shared_from_this which allows you … Read more

Why does unique_ptr instantiation compile to larger binary than raw pointer?

Because std::make_unique<int[]>(100) performs value initialization while new int[100] performs default initialization – In the first case, elements are 0-initialized (for int), while in the second case elements are left uninitialized. Try: int *p = new int[100](); And you’ll get the same output as with the std::unique_ptr. See this for instance, which states that std::make_unique<int[]>(100) is … Read more

Why is std::rc::Rc not Copy?

It seems to me that Rc<> should just consist of a pointer, which is a fixed size, so the type itself should be Sized and hence Copy, right? This is not quite true. Rc is short for Reference Counted. This means that the type keeps track of how many references point to the owned data. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)