Regarding smart pointer

Hi, guys. I just had this problem with smart pointer. My code is shown below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <memory>
using namespace std;
void func(int *p)
{
    ;
}
int main()
{
    int *x;
    {
        int *y = new int(42);
        x = y;
        shared_ptr<int> p(y, func);
    }
    cout << *x;
}


Basically, I defined a void function which did nothing. Then in the main function part I declared a built-in pointer x, then declared another built-in pointer y in a sub-block which was bound to an object whose value was 42. Later on, x was assigned to hold the same address as y did in the same sub-block and a smart pointer p was defined to hold the same address as y did as well, and that's the end of this sub-block. In the end, I printed out the value of the object that's bound to x.
Here is the problem, I was expecting the value that was printed out being an undefined value because x ,y and p were all bound to the same object valued 42, but when the program was out of the sub-block, p should be automatically destroyed, and so would the memory that it held be freed, which means that this object valued 42 would no longer exist. To my surprise, the value being printed out at last was still 42. Could someone help me out here? Thanks a lot!
1) shared_ptr p called the deleter (func) to delete y when p went out of scope at line 15 since p's reference count went to zero.
2) It's was func's responsibility to delete y, but it did not do so. Therefore x still points to valid allocated memory (a memory leak).
This isn't doing anything like you think you're doing.
All you are doing is assign one raw pointer to another and derefencing it to get a value. line 14 isn't doing anything.

The second parameter to a std::shared_ptr is the deleter. were you aware of this? "An optional deleter d can be supplied that is later used to destroy the object when no shared_ptr objects own it."

http://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr

oh, and your "sub block" isn't doing anything either.
Last edited on
The reason would partly be the fact that you are giving the shared pointer a deleter that does nothing - hence, when the pointer goes out of scope, the variable isn't actually deleted and you end up with a memory leak (assuming you don't delete x later).

Even if you did delete it, though, you might get the same result (though not necessarily), because though deleting the variable frees its memory, it doesn't necessarily have to rewrite more data over the old one - in fact, for performance reasons, it normally won't.
Hey guys, thank you all for your replies. Does it mean that if I supply a deleter which does not do deletion when I initialize a shared_ptr, then even if the count of the shared_ptr is later on decremented to 0, the object to which this shared_ptr is bound will not be freed and I can still access it by other pointers if they point to the same object.
Basically, yes. If your deleter doesn't delete it, then you don't really have a deleter, do you?
Okay, thanks for the clarification. I really appreciate it!
Topic archived. No new replies allowed.