您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        # C++常见的内存泄漏有哪些
## 引言
内存泄漏(Memory Leak)是C++开发中常见且棘手的问题,指程序在运行过程中未能释放不再使用的内存,导致系统内存逐渐耗尽。本文将系统分析C++中典型的内存泄漏场景、检测方法和预防策略。
---
## 一、内存泄漏的定义与危害
### 1.1 基本概念
内存泄漏指动态分配的内存未被正确释放,导致该内存无法被后续程序使用。在长期运行的程序(如服务器、嵌入式系统)中,累积泄漏可能引发严重问题。
### 1.2 主要危害
- **系统性能下降**:可用内存减少,可能触发频繁的磁盘交换
- **程序崩溃**:内存耗尽时引发`std::bad_alloc`异常
- **安全风险**:敏感数据可能残留在未释放的内存中
---
## 二、C++常见内存泄漏场景
### 2.1 基础指针管理失误
#### 2.1.1 new/delete不匹配
```cpp
int* ptr = new int[100];
delete ptr;  // 错误!应使用 delete[] ptr
void riskyFunction() {
    int* ptr = new int(42);
    if(some_condition) throw std::runtime_error("Oops");
    delete ptr;  // 异常发生时此句不会执行
}
std::vector<int*> vec;
vec.push_back(new int(10));
// 忘记遍历释放内存
struct Node {
    std::shared_ptr<Node> next;
    std::shared_ptr<Node> prev;  // 循环引用导致泄漏
};
class ResourceHolder {
    int* resource;
public:
    ResourceHolder() : resource(new int(100)) {}
    ~ResourceHolder() { /* 忘记delete resource */ }
};
class BadCopy {
    char* buffer;
public:
    BadCopy(const char* str) {
        buffer = new char[strlen(str)+1];
        strcpy(buffer, str);
    }
    ~BadCopy() { delete[] buffer; }
    // 缺少拷贝构造函数和赋值运算符
};
void useLibrary() {
    SomeLibHandle handle = SomeLib_Init();
    // ...使用库...
    // 忘记调用 SomeLib_Close(handle)
}
void readFile() {
    FILE* fp = fopen("data.txt", "r");
    if(!fp) return;  // 直接返回导致句柄泄漏
    // ...处理文件...
    fclose(fp);
}
| 工具名称 | 适用平台 | 特点 | 
|---|---|---|
| Valgrind | Linux | 功能强大,支持内存检查 | 
| Dr. Memory | Windows/Linux | 轻量级替代方案 | 
| Visual Studio | Windows | 内置调试器和诊断工具 | 
new必须有对应的delete#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
std::unique_ptr<int> ptr(new int(42));
// 自动释放内存
auto ptr = std::make_shared<MyClass>();
class FileWrapper {
    FILE* fp;
public:
    explicit FileWrapper(const char* fname) : fp(fopen(fname, "r")) {}
    ~FileWrapper() { if(fp) fclose(fp); }
    // 禁用拷贝(或实现深拷贝)
};
std::vector<int> data(100);  // 替代 new int[100]
void safeFunction() {
   auto ptr = std::make_unique<Resource>();
   // 即使抛出异常也能自动释放
}
atomic_shared_ptr(C++20)// 预分配内存重用
static thread_local std::vector<int> reusableBuffer;
reusableBuffer.clear();
reusableBuffer.reserve(1024);
“C++ makes it harder to shoot yourself in the foot, but when you do, it blows your whole leg off.” - Bjarne Stroustrup “`
注:本文实际约2100字(中文字符统计)。如需精确控制字数,可适当增减示例代码或调整章节深度。建议通过实际代码测试文中的示例来加深理解。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。