开发者

STL container leak

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;
PopulatorClas开发者_开发技巧s.populate(list);

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

I'm using Visual Studio 2010 (CRT).

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

Thanks, J


May be Memory leak with std::vector<std::string> or something like this.


Every time you got a problem with the STL implementation doing something strange or wrong like a memory leak, try this :

  • 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;
 }
  • 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.


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:

_Myt& assign(_Myt&& _Right)
    {        // assign by moving _Right
    if (this == &_Right)
        ;
    else if (get_allocator() != _Right.get_allocator()
        && this->_BUF_SIZE <= _Right._Myres)
        *this = _Right;
    else
        {        // not same, clear this and steal from _Right
        _Tidy(true);
        if (_Right._Myres < this->_BUF_SIZE)
            _Traits::move(this->_Bx._Buf, _Right._Bx._Buf,
                _Right._Mysize + 1);
        else
            {        // copy pointer
            this->_Bx._Ptr = _Right._Bx._Ptr;
            _Right._Bx._Ptr = 0;
            }
        this->_Mysize = _Right._Mysize;
        this->_Myres = _Right._Myres;

        _Right._Mysize = 0;
        _Right._Myres = 0;
        }
    return (*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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜