STL容器泄漏
我使用向量容器来保存包含 3 个 int 和 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)。
在展开堆栈并删除向量
时,我需要做些什么特殊的事情才能使string
正确删除吗?
谢谢, J
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是 内存泄漏
std::vector
或类似的内容。May be Memory leak with
std::vector<std::string>
or something like this.每当您在 STL 实现中遇到奇怪或错误的问题(例如内存泄漏)时,请尝试以下操作:
未针对您的特定问题测试简单的动态示例:
Every time you got a problem with the STL implementation doing something strange or wrong like a memory leak, try this :
Not tested simple on-the-fly example for your specific problem :
可能与 Alexey 的链接有相同的根本问题。发布的版本已损坏 basic_string 的移动代码。 MS抛弃了我们VC10用户,所以你必须自己修复它。在 xstring 文件中,您有以下内容:
注意最后一个
_Right._Myres = 0;
这应该只在最后一个条件下发生,因为短期情况下 _Right 最好不要管。
由于容量设置为 0 而不是 15,当您分配另一个小字符串时,其他代码将在函数 Grow() 中进行意外分支,并分配一块内存,只是为了用直接字符串内容来践踏指针。
Probably same root issue as Alexey's link. The shipped version has broken move code for basic_string. MS abandoned us VC10 users, so you must fix it yourself. in xstring file you have this:
Note the last
_Right._Myres = 0;
that should happen only under the last condition, for the short case _Right should better be left alone.
As the capacity is set to 0 instead of 15, other code will take unintended branch in function Grow() when you assign another small string and will allocate a block of memory just to trample over the pointer with the immediate string content.