44

I'm new to shared_ptr's and I'm trying to figure out the exact functionality of the .reset() function.

#include <memory>
#include <stdio>

using namespace std;
class SomeClass{};

int main() 
{
   shared_ptr<SomeClass> sp (nullptr);

   //do some stuff, sp now has 10 co-owners

   cout << sp.use_count << endl;
   sp.reset();
   cout << sp.use_count << endl;
   return 0;
}

Would output

10
0

So since I used the reset function are all instances deleted from memory? As in, have I just eliminated any possible memory leaks with sp? Obviously this was a toy example that I quickly made up, sorry if it has any errors.

Follow up situation:

shared_ptr<SomeClass> returnThis() {
    shared_ptr<SomeClass> someObject(new SomeClass(/*default constructor for example*/) );
    return someObject;
}

somehere in main:

shared_ptr<SomeClass> mainObject;
mainObject = returnThis();

Does mainObject have a use count of 2 because someObject was created in a function but never cleared? Or is it one and the clean-up is done automatically when returning the value?

zeus_masta_funk
  • 1,388
  • 2
  • 11
  • 34
  • Whoops! If you want to ask a follow-up question, use the "Ask Question" button at the top of the page. The short story is that `shared_ptr` exists to automatically handle all of these things, so when the *last* shared pointer leaves scope, the object is destroyed. – Dietrich Epp Feb 07 '14 at 17:40
  • [Equivalent to shared_ptr().swap(*this);](https://en.cppreference.com/w/cpp/memory/shared_ptr/reset) – Aykhan Hagverdili Feb 03 '19 at 18:02

3 Answers3

67

When you use .reset(), you are eliminating one owner of the pointer, but all of the other owners are still around. Here is an example:

#include <memory>
#include <cstdio>

class Test { public: ~Test() { std::puts("Test destroyed."); } };

int main()
{
    std::shared_ptr<Test> p = std::make_shared<Test>();
    std::shared_ptr<Test> q = p;
    std::puts("p.reset()...");
    p.reset();
    std::puts("q.reset()...");
    q.reset();
    std::puts("done");
    return 0;
}

The program output:

p.reset()...
q.reset()...
Test destroyed.
done

Note that p and q are both owners of the object, and once both p and q are reset, then the instance is destroyed.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    Thanks for the answer! As a follow up question, say I have a function that's return type is a std::shared_ptr but the shared_ptr that I'm returning was created inside that function, will returning the shared_ptr automatically decrement its use_count by 1? I'll post a follow up example above. – zeus_masta_funk Feb 07 '14 at 16:02
18

No.

The whole purpose of shared_ptr is that you cannot delete it from one place if someone is using it in another. shared_ptr::reset() just decreases use_count by one and replaces its object by nullptr.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Alex Telishev
  • 2,264
  • 13
  • 15
2

The .reset() method only applies to the object it's called upon.

It just replaces the pointer that variable is holding.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185