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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:16:29+00:00 2026-05-13T11:16:29+00:00

In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team

  • 0

In my current project I am using boost::shared_ptr quite extensively.

Recently my fellow team mates have also started using weak_ptr. I don’t know which one to use and when.

Apart from this, what should I do if I want to convert weak_ptr to shared_ptr. Does putting a lock on weak_ptr to create a shared_ptr affect my code in other thread?

  • 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-13T11:16:29+00:00Added an answer on May 13, 2026 at 11:16 am

    In general and summary,

    Strong pointers guarantee their own validity. Use them, for example, when:

    • You own the object being pointed at; you create it and destroy it
    • You do not have defined behavior if the object doesn’t exist
    • You need to enforce that the object exists.

    Weak pointers guarantee knowing their own validity. Use them, for example, when:

    • You access it, but it’s not yours.
    • You have defined behavior if the object doesn’t exist

    Lock() on a weak pointer returns a strong pointer; this is how you access the weak pointer. If the object is no longer valid (it’s been deleted, etc), then the strong pointer will be NULL, otherwise, it will point at the object. You will need to check this.

    It’s set up this way so that you cannot accidentally delete the object while you’re using it, because you’ve made a temporary (local) strong pointer, and thus garunteed the object’s existence while that strong pointer remains. When you’re done using the object, you generally let the strong pointer fall out of scope (or reassigning it), which then allows the object to be deleted. For multithreading, treat them with same care you treat other things that don’t have built-in thread safety, noting that the guarantee I mentioned above will hold when multithreading. AFAIK they don’t do anything special past that.

    The boost shared pointers also have garbage-collector like features, since when the last strong pointer to an object goes away or points somewhere else, the object gets deleted.

    There’s also the performance and circular dependencies mentioned in the other answers.

    Fundamentally, I would say that the boost shared pointer library allows you to not mess up putting together a program, but it is no substitute for taking the time to properly design your pointers, object ownerships and lifetimes. If you have such a design, you can use the library to enforce it. If you don’t have such a design, you’re likely to run into different problems than before.

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

Sidebar

Related Questions

I am using a Cairngorm MVC architecture for my current project. I have several
I've used Trac/Subversion before and really like the integration. My current project is using
In my current project we are testing our ASP.NET GUI using WatiN and Mbunit
I've been using WatiN as a testing tool for my current project. Besides the
On my current project, we've been using Struts 1 for the last few years,
In my current project, I'm producing weekly releases. I've been using the technique described
My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
My current project uses NUnit for unit tests and to drive UATs written with
My current project involves deploying an upgraded .exe file that runs as a Windows
My current project involves writing Perl code inside a Solaris VMWare appliance (hosted on

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.