在类构造函数中初始化映射时,如何避免内存泄漏

How can I avoid a memory leak when initializing a map in the constructor of a class?

本文关键字:何避免 内存 泄漏 映射 构造函数 初始化      更新时间:2023-10-16

我想在类的构造函数中初始化(指向A(map的指针。我编写编译的程序,但由于细分故障而在运行时失败。我可以通过动态的map分配内存来解决问题,但Valgrind告诉我内存泄漏。如何正确初始化课程?

这是一个示例

#include <iostream>
#include <map>
#include <string>
#include <vector>
class MemoryLeak {
   public:
    MemoryLeak(std::vector<std::string>& inp) {
        int i = 0;
        std::map<std::string, int>* tmp = new std::map<std::string, int>;
        for (std::string& s : inp) {
            //(*problem_map)[s] = i++; // Line 12: causes a seg fault
            (*tmp)[s] = i++;
        }
        problem_map = tmp;  // Line 15: memory leak
    }
    std::map<std::string, int>* problem_map;
};
int main() {
    std::vector<std::string> input{"a", "b"};
    MemoryLeak mem = MemoryLeak(input);
    for (auto const& it : *(mem.problem_map)) {
        std::cout << it.first << ": " << it.second << "n";
    }
    return 0;
}

当我删除line 12(并注释Line 15(时,该程序会编译,但似乎发生了内存泄漏。有人可以告诉我我在做什么错吗?更合适的构造函数将如何?

segfault

您的指针problem_map在第12行中未经启发。这就是为什么segfault。您不需要tmp您只能执行此操作:

problem_map = new std::map<std::string, int>;
for (std::string& s : inp) {
    (*problem_map)[s] = i++; 
}

现在泄漏,您有两个选择:

1(添加一个驱动器,一个复制构建器和复制分配操作员(或使其删除(。请参阅三个规则

class MemoryLeak {
   public:
    ~MemoryLeak() {
        delete problem_map;
    }
    MemoryLeak(const MemoryLeak& ) = delete;
    MemoryLeak& operator=(const MemoryLeak& ) = delete;
    MemoryLeak(std::vector<std::string>& inp) {
        int i = 0;
        problem_map = new std::map<std::string, int>;
        for (std::string& s : inp) {
           (*problem_map)[s] = i++; 
        }
    }
    std::map<std::string, int>* problem_map;
};

2(不要存储指针,而是地图

class MemoryLeak {
   public:
    MemoryLeak(std::vector<std::string>& inp) {
        int i = 0;
        for (std::string& s : inp) {
            problem_map[s] = i++;
        }
    }
    std::map<std::string, int> problem_map;
};