Disadvantages of smart pointers and problems caused by smart pointers

Disadvantages of smart pointers

A smart pointer is a tool for managing dynamic memory that automates the allocation and deallocation of memory, thereby reducing the possibility of programmer errors in manually managing memory. However, smart pointers also have some disadvantages, including the following:

1. Performance overhead: Smart pointers require additional memory management and reference counting operations, which may lead to program performance degradation. Compared with raw pointers, smart pointers require more computing resources and time to complete memory management tasks.

2. Circular references: If smart pointers are used to manage circular references between objects, memory leaks may occur. When two objects refer to each other, their reference counts never reach zero, so their memory is never freed.

3. Difficult to debug: Since the memory managed by the smart pointer is automatically allocated and released, it is difficult to determine which pointer points to which memory block when the program is running, and which pointer may cause problems such as memory leaks or dangling pointers. This makes debugging very difficult.

4. Not suitable for some scenarios: Smart pointers are usually suitable for single-threaded environments, but in some multi-threaded or asynchronous environments, the use of smart pointers may cause problems such as race conditions or deadlocks. In addition, smart pointers are not suitable for scenarios where memory needs to be shared between different processes.

To sum up, although smart pointers can reduce the workload of memory management in some cases, they also have some disadvantages and need to be selected according to specific application scenarios.

#include<iostream>
#include<memory>
using namespace std;

struct Test
{
    shared_ptr<Test>getSharedPtr()
    {
        return shared_ptr<Test>(this);
    }

    ~Test()
    {
        cout<<"析构函数"<<endl;
    }
};

int main()
{
    shared_ptr<Test> sp1(new Test);
    cout<<"引用个数"<<sp1.use_count()<<endl;
    shared_ptr<Test> sp2 = sp1->use_count()<<endl;
    cout<<"引用个数"<<sp1.use_count()<<endl;
    return 0;
}

//引用个数:1
//引用个数:1
//析构函数
//析构函数

Through the output results, it can be seen that an object has been destructed twice. The reason is as follows: In this example, two smart pointer objects sp1 and sp2 are constructed using the same pointer this, and there is no object between them. Relational, because sp2 does not get the instance object through sp1 initialization. After leaving the scope, this will be destructed by the two smart pointers constructed, resulting in a double-destruction error.

#include<iostream>
#include<memory>
using namespace std;

struct Test
{
    shared_ptr<Test>getSharedPtr()
    {
        return shared_ptr<Test>(this);
    }

    ~Test()
    {
        cout<<"析构函数"<<endl;
    }
};

int main()
{
    shared_ptr<Test> sp1(new Test);
    cout<<"引用个数"<<sp1.use_count()<<endl;
    shared_ptr<Test> sp2 = sp1;
    cout<<"引用个数"<<sp1.use_count()<<endl;
    return 0;
}

//引用个数:1
//引用个数:2
//析构函数

circular reference problem

#include<iostream>
#include<memory>
using namespace std;

class A;
class B;

classA
{
public:
    shared_ptr<B> bptr;
    ~A()
    {
        cout<<"class TA is disstruct..."<<endl;
    }
};

class B
{
public:
    shared_ptr<A> aptr;
    ~B()
    {
    cout<<"class TB is disstruct..."<<endl;
    }

void testPtr()
{
    shared_ptr<A> ap(new A);
    shared_ptr<B> bp(new B);
    cout<<"A的 引用计数:"<<ap.use_count()<<endl;
    cout<<"B的 引用计数:"<<bp.use_count()<<endl;
}
}

int main()
{
    testPtr();
    return 0;
}

A的引用计数:1
B的引用计数:1
A的引用计数:2
B的引用计数:2

The reference counts of the shared smart pointers ap and bp to the instance objects of A and B become 2. After the shared smart pointer leaves the scope, the reference count can only be reduced to 1. In this case, the smart pointer management memory will not be deleted, resulting in class The instance objects of A and B cannot be destructed, which will eventually cause memory leaks. This problem can be solved by using weak_ptr, just change any member of class A or class B to weak_ptr.

class A
{
public:
    weak_ptr<B>bptr;
    ~A()
    {
        cout<<"A的析构函数"<<endl;
    }
};

class B
{
public:
    shared_ptr<A> aptr;
    ~B()
    {
        cout<<"B的析构函数"<<endl;
    }
};

void testPtr()
{
    shared_ptr<A> ap(new A);
    shared_ptr<B> bp(new B);
    cout<<"A 的引用计数:"<<ap.use_count()<<endl;
    cout<<"B 的引用计数:"<<bp.use_count()<<endl;

    ap->bptr = bp;
    bp->aptr = ap;
    cout<<"A 的引用计数:"<<ap.use_count()<<endl;
    cout<<"B 的引用计数:"<<bp.use_count()<<endl;
}

 

Guess you like

Origin blog.csdn.net/four_two_six_/article/details/130887975
Recommended