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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:25:34+00:00 2026-05-29T09:25:34+00:00

I have an STL map that contains shared pointers to objects that are manipulated

  • 0

I have an STL map that contains shared pointers to objects that are manipulated in multiple threads. The object the shared_ptr owns is being deleted while a smart pointer is still present in the container and/or the scope of other functions. All shared_ptr instances are passed by value (so its not a problem with a misuse of references). I was under the impression from this source here that as long as an instance of shared_ptr exists (that was copied from an existing shared_ptr) the object it owns will not be deallocated.

Here is basically what I’m doing:

/* Remove Event response in Thread A */
std::map<std::string, std::shared_ptr<object>>::iterator it = objects.find(id);
if(it != objects.end())
{
    std::shared_ptr<object> ob = it->second;
    objects.erase(it);

    //Do cleanup work with ob
}

/* Add Event response in Thread B */
std::map<std::string, std::shared_ptr<object>>::iterator it = objects.find(id);
if(it == objects.end())
{
    std::shared_ptr<object> ob(new object(id));
    objects[id] = ob;

    //Do setup work with ob
}

/* Duty Cycle Event response in Thread C (very frequent) */
//Take snapshot of objects so Remove Event does not invalidate iterators of duty cycle
std::map<std::string, std::shared_ptr<object>> temp_objects = objects; 
for(std::map<std::string, std::shared_ptr<object>>::const_iterator it = temp_objects.begin(); it != temp_objects.end(); ++it)
{
    std::shared_ptr<object> ob = it->second;
    //Access violation can (but doesn't always) occur when dereferencing ob (ob is empty)
}

What am I doing wrong? Am I misusing shared pointers or making improper assumptions about how they operate?

  • 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-29T09:25:34+00:00Added an answer on May 29, 2026 at 9:25 am

    The reference count for shared_ptr objects is atomic; this is required by C++11. However, what is not required is that std::map operations are atomic. They’re certainly not.

    It is not guaranteed that you can insert from one thread and delete from another and still maintain the integrity of the contents of the map. So unless you put in your own mutexes or some other synchronization, to prevent race conditions on the map itself, your code will break.

    This has nothing to do with shared_ptr; that’s just the symptom of the problem. This is about the map that contains them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a STL map where the values are pointers, and I want
I have a map that relates integers to vectors (of objects). These vectors represent
I have the following code that saves a map into shared memory using boost
I have an STL map that I want to iterate through, and can't seem
If i have a stl map from string to int and i want to
How can I implement STL map sorting by value? For example, I have a
I have two STL containers that I want to merge, removing any elements that
I have a stl::list containing Widget class objects. They need to be sorted according
I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead
I am working on a large project that uses the STL and have a

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.