Search code examples
c++shared-ptrsmart-pointers

Is it required to reset the shared_ptr object initialized in a function(local variable)? and global to the file?


Initialized global static shared_ptr object and passed to a function:

#include <iostream>
#include <memory>

std::shared_ptr<int> globalObj = std::make_shared<int>(5);

int testFunc(std::shared_ptr<int> obj1) {
    std::shared_ptr<int> localObj = obj1;
    std::cout<<"localObj.count:" << localObj.use_count() << std::endl;
    std::cout << "localObj:" << *localObj <<std::endl;
    localObj.reset();  -->** is it necessary to reset local count??**
    std::cout<<"localObj.count:" << localObj.use_count() << std::endl;
    return 0;
}

int main()
{
    std::cout<<"globalObj.count:" << globalObj.use_count() << std::endl;
    std::cout <<"globalObj:" << *globalObj <<std::endl;
    
    testFunc(globalObj);
    
    std::cout<<"globalObj.count:" << globalObj.use_count() << std::endl;
    std::cout <<"globalObj:" << *globalObj <<std::endl;
    
    globalObj.reset(); --> **is it necessary to reset global count??**
    std::cout<<"globalObj.count:" << globalObj.use_count() << std::endl;

    return 0;
}

globalObj.count:1
globalObj:5
localObj.count:3
localObj:5
localObj.count:3
globalObj.count:1
globalObj:5
globalObj.count:1

Does reset() affect the efficiency of the C++ program?


Solution

  • There is no need to reset, No.
    The goal of the smart pointers: unique_ptr, shared_ptr and weak_ptr is to make automatic the release of the memory, through RAII concept.

    Releasing the smart pointer is, consequently, pointless in 99.9% of the cases.

    Here is an example of your code with few clarifications:

    
    
    #include <iostream>
    #include <memory>
    
    class MyInt;
    auto globalObj = std::make_shared<MyInt>(5);
    
    class MyInt final
    {
        int intVal;
    public:
        MyInt()
        {
            std::cout << "Constucted!" << std::endl; 
        }
        
        MyInt(int nv ): intVal(nv)
        {
            std::cout << "Constucted!" << std::endl;
        }
        operator int()
        {
            return intVal; 
        }
        ~MyInt()
        {
            std::cout << "Destroyed!" << std::endl;
        }
    };
    
    int testFunc(const std::shared_ptr<MyInt>& obj1) 
    {
        using std::cout;
        using std::endl;
        
        auto localObj = obj1;
        cout<<"localObj.count:" << localObj.use_count() << endl;
        cout << "localObj:" << *localObj << endl;
        return 0;
    }
    
    int main()
    {
        using std::cout;
        using std::endl;
        
        {
        cout<< "globalObj.count:" << globalObj.use_count() << endl;
        cout << "globalObj:" << *globalObj << endl;
        
        testFunc(globalObj);
        
        cout << "globalObj.count:" << globalObj.use_count() << endl;
        cout << "globalObj:" << *globalObj << endl;
        }
    
        return 0;
    }
    

    Output:

    Constucted!
    globalObj.count:1
    globalObj:5
    localObj.count:2
    localObj:5
    globalObj.count:1
    globalObj:5
    Destroyed!

    You can test it here: https://onlinegdb.com/tXqnoysmT