Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:26:15+00:00 2026-05-18T20:26:15+00:00

I’m using a vector container to hold instances of an object which contain 3

  • 0

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:26:15+00:00Added an answer on May 18, 2026 at 8:26 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
In order to apply a triggered animation to all ToolTip s in my app,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.