1

I know this is a pretty common question, but I do not understand the code examples from related questions (Too long to work into, too complicated). So, here goes my simple Code snippet:

#include <iostream>
#include <memory>
#include <vector>

using namespace std;

int main(){
  vector<int> v({1,2,3,4,5});                 
  shared_ptr<vector<int>> v_ptr(&v);

  cout << (*v_ptr)[2] << endl;

}

Now, this short snippet creates a memory leak. I think this is caused by destructing the vector before destructing the shared_ptr. However, I do not understand how to destroy the pointer accordingly. Simply trying reset() won't work, so what can I do, and why is there a memory leak?

Please, don't ask me why this is needed, I am new to smart pointers and trying to understand how to use them. This example is independent from my work, however, I want to avoid memory leaks in the future, so this question might actually help. Thanks in advance!

ro_go
  • 90
  • 7
  • “this short snippet creates a memory leak” — what makes you think that? – Konrad Rudolph Dec 16 '19 at 10:48
  • @tzaman I doubt it, the question you link is highly technical, and understanding the answers definitely requires a more advanced understanding of C++. – Konrad Rudolph Dec 16 '19 at 10:50
  • @KonradRudolph it is exactly the situation presented here - creating a shared_ptr to a stack object is the problem, and the answers there outline a few different solutions (using an empty deleter, or `make_shared`, or `new` instead of stack allocation). Reading through it would be valuable for someone trying to learn why this is a problem. – tzaman Dec 16 '19 at 10:55
  • @tzaman The question *might* be the same (or similar enough anyway) but the answers are addressed at vastly different audiences. It’s completely inappropriate for OP. – Konrad Rudolph Dec 16 '19 at 10:59
  • @KonradRudolph Several answers there that are pretty clear, notably Pete Becker but also a couple of the others; even the accepted one I wouldn't call "too advanced". OP is starting to use smart pointers, at which point learning about object ownership and lifetimes becomes _necessary_, not esoteric, since managing those is their entire purpose. I'd rather not make assumptions about OP's lack of competence, specially when they've indicated a desire to learn. – tzaman Dec 16 '19 at 11:48
  • @tzaman Fair enough … though I disagree with you regarding the accepted answer: in fact that answer is just bad because it’s misleading and 100% a red herring. – Konrad Rudolph Dec 16 '19 at 11:52
  • @tzaman: I like to agree with your opinion. I found other threads related to my question (as I indicated..) But I couldn't really make something of the answers, because the codes was too complicated for a new programmer (like me). Thank you for your answers anyway! using make_shared() worked fine :-) – ro_go Dec 16 '19 at 14:04

2 Answers2

3

Because shared_ptr calls delete when the object has no more references left (unless you use a deleter to make it do something else instead), it should only be used with objects allocated with new (or make_shared, which calls new).

v was not allocated with new so you should not use it with shared_ptr.

user253751
  • 57,427
  • 7
  • 48
  • 90
1

The vector v is an object allocated on the stack. Therefor, when it goes out of scope, it is automatically destroyed. When you give its address to the smart pointer, it tries to delete it when the pointer goes out of scope. So there are both of your frees.

Rokas Višinskas
  • 533
  • 4
  • 12