CppCoreGuidelines C.149 new 对象的时候,用 unique_ptr 或 shared_ptr,避免忘记删除对象
06 January 2023
C.149: Use unique_ptr
or shared_ptr
to avoid forgetting to delete
objects created using new
理由
避免资源泄漏。
例子
void use(int i) { auto p = new int {7}; // bad: initialize local pointers with new auto q = make_unique<int>(9); // ok: guarantee the release of the memory-allocated for 9 if (0 < i) return; // maybe return and leak delete p; // too late }
class Flower { public: Flower(int id):m_id{id} { cout << "new flower " << m_id << "\n"; } ~Flower() { cout << "delete flower " << m_id << "\n"; } private: int m_id{0}; }; void use(int i) { auto p = new Flower {7}; // bad: initialize local pointers with new auto q = make_unique<Flower>(9); // ok: guarantee the release of the memory-allocated for 9 if (0 < i) return; // maybe return and leak delete p; // too late } int main() { use(0); use(42); }
new flower 7 new flower 9 delete flower 7 delete flower 9 new flower 7 new flower 9 delete flower 9
以上代码,如果提前返回了,flower 7
就没有被删除,内存泄漏!
强化
- 标记使用原始指针接受
new
返回的结果 - 标记对局部变量进行
delete
的操作