我正在使用 vector 容器来保存包含 3 个整数和 2 个 std::string
的对象的实例,这是在堆栈上创建的并从另一个类中的函数填充但是通过 deleaker 运行应用程序显示对象中的 std::string
全部泄漏。这是代码:
// Populator function:
void PopulatorClass::populate(std::vector<MyClass>& list) {
// m_MainList contains a list of pointers to the master objects
for( std::vector<MyClass*>::iterator it = m_MainList.begin(); it != m_MainList.end(); it++ ) {
list.push_back(**it);
}
}
// Class definition
class MyClass {
private:
std::string m_Name;
std::string m_Description;
int m_nType;
int m_nCategory;
int m_nSubCategory;
};
// Code causing the problem:
std::vector<MyClass> list;
PopulatorClass.populate(list);
当它通过 deleaker 运行时,泄漏的内存位于 std::string
类的分配器中。
我使用的是 Visual Studio 2010 (CRT)。
在展开堆栈和删除 vector
时,我需要做些什么才能正确删除 string
吗?
谢谢, J
最佳答案
可能是Memory leak with std::vector<std::string>
或类似的东西。
关于c++ - STL容器泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4494012/