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 6534809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:17:46+00:00 2026-05-25T10:17:46+00:00

trying to solve a mem leak I’ve changed this: void addListener(IListener* listener, const std::string&

  • 0

trying to solve a mem leak I’ve changed this:

void addListener(IListener* listener, const std::string& appender )
{
     std::pair<t_ListenerVectorMap::iterator,bool> p =
    listenerVectorMap.insert(std::string(appender), new t_ListenerVector);
    p.first->second->push_back(listener);

    //add the appender
    addAppender(appender);
}

By this:

void addListener( IListener* listener,const std::string& appender )
{
    t_ListenerVector* lv = new t_ListenerVector;
    std::pair<t_ListenerVectorMap::iterator,bool> p =
    listenerVectorMap.insert(std::string(appender), lv);

    p.first->second->push_back(listener);
    if (!p.second)
    {
        delete lv;
    }

    addAppender(appender);
}

The leak I’m trying to get ride is the new t_ListenerVector (is a boost::ptr_vector). When appender already exists into listenerVectorMap, then insert operation doesn’t not modify contents of map, so new t_ListenerVector isn’t inserted and becomes a mem leak.

The change is to avoid this leak. If no new insertion as been done, lv is not inserted so I do remove it. But the problem is that delete lv throws an assertion (in vc\include\vector Line:70).
If lv hasn’t been changed, why this error?

Some ideas are welcome.
Thanks

  • 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-25T10:17:47+00:00Added an answer on May 25, 2026 at 10:17 am

    For this answer I am assuming that the type of t_ListenerVectorMap is boost::ptr_map<std::string, boost::ptr_vector<IListener>>. If it isn’t then this answer is probably worthless.


    You have a misunderstanding about how boost::ptr_map works. It is true that your original code contains a memory leak, but your replacement code is even worse. listenerVectorMap.insert(std::string(appender), lv) makes listenerVectorMap take ownership of lv, regardless of whether lv is inserted or not. This means that the delete lv; line results in a double-deletion.

    To fix the memory leak in the first version of your code, all you have to do is this:

    void addListener(IListener* listener, const std::string& appender )
    {
        std::auto_ptr<IListener> ls(listener);
        //The original code leaked here if the std::string constructor threw.
        std::pair<t_ListenerVectorMap::iterator,bool> p =
            listenerVectorMap.insert(appender, new t_ListenerVector());
        p.first->second->push_back(ls);
    
        //add the appender
        addAppender(appender);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been trying to solve this one for hours... string date = 2009-09-23T13:00:00 DateTime t
Trying to solve this problem . I would like to learn how the bootstrapper
I was trying to solve my XNA Font problem , when I found this
I'm trying to solve this flickering problem on the iphone (open gl es game).
I have been trying to solve this problem for a while, could not find
im trying to solve this issue. I have a web site with this simplified
Been trying to solve this for a while now. I need a regex to
I've been trying to solve this, and have been getting stuck, so I thought
I have been trying to solve this problem for three days now, it's really
I'm trying to solve this: A user class with 50 fields is defined in

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.