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

  • Home
  • SEARCH
  • 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 913861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:37:35+00:00 2026-05-15T17:37:35+00:00

I have some shared pointer shared_ptr<T> pointer1(new T(1)); . Now, in some other part

  • 0

I have some shared pointer shared_ptr<T> pointer1(new T(1));.

Now, in some other part of code I have an explicit copy of pointer2 (guess it would be stored in a std::map or some other container). Let’s say that copy was done like map.insert(make_pair(key1, pointer1));.

I am using that second copy only to precache some data and this means that if the main pointer is already invalid, there is no need to store the second pointer. What should I do in this case?

Is there any way to force the memory deallocation for the second pointer if I know that pointer1 became invalid in some other part of my code?

Or should I take the ugly way – from time to time check my map for pointers which have ptr.unique() set to true and destruct them?

Maybe some alternatives / advices?


Edit – plain code sample

std::map<int, shared_ptr<int> > map;

{
   shared_ptr<int> pointer1(new int(5));
   map.insert(std::make_pair(0, pointer1));
}

Is there any way / trick to make map contain <0, shared_ptr[NULL]> instead of <0, shared_ptr[5]> after these operations happen?

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-15T17:37:35+00:00Added an answer on May 15, 2026 at 5:37 pm

    It sounds like this is a task for weak_ptr:

    http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/weak_ptr.htm

    Try putting them in your table instead of a shared_ptr.

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

Sidebar

Related Questions

I have some shared code I want to reuse in my Xcode projects. The
I have some code shared among multiple projects in a static library. Even with
I have some projects on bzr code repositories shared with colleagues. Problem is, I
I have some auto-generated HTML code. When I add a float:left; on the shared
Can anyone help? I have some code that is shared between 2 projects. The
I have been using the following code for quite some testing now and havent
I have a shared site in sharepoint and have some RSS feed webpart in
I have a big project compiled into libProject.so-file (shared library), I made some modules
A question about boost::shared_ptr here: I have 3 Classes. A is some kind of
I'm rewriting my code with smart pointer. I have such situation: void Foo(SomeClass *p)

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.