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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:44:03+00:00 2026-05-20T10:44:03+00:00

I have a vector of pointers to objects created with new . Multiple threads

  • 0

I have a vector of pointers to objects created with new. Multiple threads access this vector in a safe manner with various gets/sets. However, a thread may delete one of the objects, in which case another thread’s pointer to the object is no longer valid. How can a method know if the pointer is valid? Options 1 and 2 actually seem to work well. I don’t know how they will scale. What is the best approach? Is there a portable version 3?

Testing for pointer validity examples that work:

1. Use integers instead of pointers. A hash (std::map) checks to see if the pointer is still valid. Public methods look like:

get(size_t iKey)
{
   if((it = mMap.find(iKey)) != mMap.end())
   {
       TMyType * pMyType = it->second;
       // do something with pMyType
   }
}

2. Have a vector of shared_ptr. Each thread tries to call lock() on its weak_ptr. If the returned shared_ptr is null we know someone deleted it while we were waiting. Public methods looks like:

get(boost::weak_ptr<TMyType> pMyType)
{
    boost::shared_ptr<TMyType> pGOOD = pMyType.lock();
    if (pGOOD  != NULL)
    {
         // Do something with pGOOD
    }
} 

3. Test for null on plain raw pointers? Is this possible?

get(TMyType * pMyType)
{
    if(pMyType != NULL){ //do something }
}
  • 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-20T10:44:03+00:00Added an answer on May 20, 2026 at 10:44 am

    #3 will not work. Deleting a pointer and setting it to NULL doesn’t affect other pointers that are pointing to the same object. There is nothing you can do with raw pointers to detect if the object is deleted.

    #1 is effectively a pointer to a pointer. If you always access it through that pointer and are able to lock it. If not, what happens if it’s deleted in another thread after you successfully get it?

    #2 is the standard implementation of this kind of idea, and the pattern is used in many libraries. Lock a handle, getting a pointer. If you get it, use it. If not, it’s gone.

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

Sidebar

Related Questions

I have created a BST full of WordInfo objects that have a vector to
I have a tree of objects, where each object has a std::vector of pointers
I want to have a deep copy of an vector with pointers to objects,
Let's say I have a container (std::vector) of pointers used by a multi-threaded application.
I have a vector that I want to insert into a set . This
I have a vector-like class that contains an array of objects of type T
I have found this example on StackOverflow: var people = new List<Person> { new
I have a vector with 1000 nodes if(count + 1 > m_listItems.capacity()) m_listItems.reserve(count +
I have 2 vector of with one has vec1{e1,e2,e3,e4} and the other one with
I have a vector of integers and I want to convert it to 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.