☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

对象申请的资源应在析构函数中释放

2.9 ID_memberDeallocation
目录 › next › previous

对象在析构函数中释放自己申请的资源是 C++ 程序设计的重要原则,不可被遗忘,也不应要求用户释放。

示例:

class A {
    int* p = nullptr;

public:
    A(size_t n): p(new int[n]) {
    }

   ~A() {  // Non-compliant, must delete[] p
    }
};

例中成员 p 与内存分配有关,但析构函数为空,不符合本规则要求。

相关

ID_memoryLeak ID_resourceLeak

参考

C++ Core Guidelines C.31 C++ Core Guidelines E.6
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.