STL容器泄漏 [英] STL container leak

查看:40
本文介绍了STL容器泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个向量容器来保存一个包含 3 个整数和 2 个 std::string 的对象的实例,这是在堆栈上创建的,并从另一个类中的函数填充,但是通过 deleaaker 运行应用程序显示对象中的 std::string 全部泄漏.代码如下:

I'm using a vector container to hold instances of an object which contain 3 ints and 2 std::strings, this is created on the stack and populated from a function in another class but running the app through deleaker shows that the std::strings from the object are all leaked. Here's the code:

// 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);

当这通过 deleaaker 运行时,泄漏的内存位于 std::string 类的分配器中.

When this is run through deleaker the leaked memory is in the allocator for the std::string classes.

我使用的是 Visual Studio 2010 (CRT).

I'm using Visual Studio 2010 (CRT).

在展开堆栈和删除 vector 时,我需要做些什么来使 string 正确删除吗?

Is there anything special I need to do to make the strings delete properly when unwinding the stack and deleting the vector?

谢谢,J

推荐答案

每当你在 STL 实现中遇到一些奇怪或错误的事情时,比如内存泄漏,试试这个:

  • 重现您尝试实现的最基本示例.如果它运行时没有泄漏,那么问题在于您填充数据的方式.这是最可能的问题来源(我指的是您自己的代码).
  • Reproduce the most basic example of what you try to achieve. If it runs without a leak, then the problem is in the way you fill the data. It's the most probable source of problem (I mean your own code).

未针对您的特定问题测试简单的即时示例:

Not tested simple on-the-fly example for your specific problem :

#include <string>
#include <sstream>


// Class definition
struct MyClass  { // struct for convenience
    std::string m_Name;
    std::string m_Description;
    int m_nType;
    int m_nCategory;
    int m_nSubCategory;
};


// Prototype of populator function:
void populate(std::vector<MyClass>& list)
{
    const int MAX_TYPE_IDX = 4;
    const int MAX_CATEGORY_IDX = 8;
    const int MAX_SUB_CATEGORY_IDX = 6;

    for( int type_idx = 0; type_idx < MAX_TYPE_IDX ; ++type_idx)
        for( int category_idx = 0; category_idx < MAX_CATEGORY_IDX ; ++category_idx)
             for( int sub_category_idx = 0; sub_category_idx < MAX_SUB_CATEGORY_IDX ; ++sub_category_idx)
             {
                   std::stringstream name_stream;
                   name_stream << "object_" << type_idx << "_" << category_idx << "_" << sub_category_idx ;
                   std::stringstream desc_stream;
                   desc_stream << "This is an object of the type N°" << type_idx << ".\n";
                   desc_stream << "It is of category N°" << category_idx << ",\n";
                   desc_stream << "and of sub-category N°" << category_idx << "!\n";

                   MyClass object;
                   object.m_Name = name_stream.str();
                   object.m_Description = desc_stream.str();
                   object.m_nType = type_idx;
                   m_nCategory = 
                   m_nSubCategory = 
                   list.push_back( object );
             }
}


int main()
{
    // Code causing the problem:
    std::vector<MyClass> list;
    populate(list);

    // memory leak check?
    return 0;
 }

  • 如果您仍然遇到内存泄漏,首先检查它是否不是来自泄漏检测软件的误报.
  • 如果不是,请谷歌搜索 STL 实现的内存泄漏问题(大部分时间在编译器开发者网站上).实施者可能会提供错误跟踪工具,您可以在其中搜索相同的问题和潜在的解决方案.
  • 如果您仍然找不到泄漏的源头,也许尝试使用不同的编译器(如果可以)构建您的项目,看看是否有相同的效果.同样,如果泄漏仍然发生,则问题很可能来自您的代码.
    • If you still got the memory leak, first check that it's not a false-positive from your leak detection software.
    • Then if it's not, google for memory leak problems with your STL implementation (most of the time on the compiler developer website). The implementor might provide a bug tracking tool where you could search in for the same problem and potential solution.
    • If you still can't find the source of the leak, maybe try to build your project with a different compiler (if you can) and see if it have the same effect. Again if the leak still occurs, the problem have a lot of chances to come from your code.
    • 这篇关于STL容器泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆