pros and cons of smart pointers

前端 未结 10 1930
长情又很酷
长情又很酷 2021-02-05 05:31

I came to know that smart pointer is used for resource management and supports RAII.

But what are the corner cases in which smart pointer doesn\'t seem smart and things

10条回答
  •  梦谈多话
    2021-02-05 06:18

    Smart pointers don't help against loops in graph-like structures.

    For example, object A holds a smart pointer to object B and object B - back to object A. If you release all pointers to both A and B before disconnection A from B (or B from A) both A and B will hold each other and form a happy memory leak.

    Garbage collection could help against that - it could see that both object are unreachable and free them.

提交回复
热议问题