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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:08:08+00:00 2026-05-29T04:08:08+00:00

I was somewhat surprised to find that std::shared_ptr provides no protection (eg, throw an

  • 0

I was somewhat surprised to find that std::shared_ptr provides no protection (eg, throw an exception) against situations when its pointed-to object has somehow been deleted. I imagine this is for performance reasons; assuming shared_ptr is doing its job the object the shared_ptr points to should have never been deleted, so it’d be silly to waste the cycles constantly checking.

I know I can check explicitly for whether a shared_ptr is valid, but if shared_ptr is “doing its job” to maintain object lifetime it would seem overkill to explicitly check every time I touch a shared_ptr.

So my question is, how cautious should I be in light of this? Is there a “rule of thumb” as to if, how often, or when I should check the shared_ptr?

My best conclusion so far would mimic Java: Any time you’re handed a reference to an object in Java that you didn’t create, you should check it for null. Would this be a good policy for shared_ptr?

  • 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-29T04:08:08+00:00Added an answer on May 29, 2026 at 4:08 am

    Just like any other pointer, check it before you use it if you have any reason to suspect that it might wrap a null pointer.

    shared_ptr does help to manage the lifetime of an object that it points to, but that is a completely separate task from deciding whether it does point to an object.

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

Sidebar

Related Questions

Somewhat related to my question about integers instead of decimals; my vendor provides a
Somewhat similar to this question , except we haven't decided that we're going with
Even as a somewhat seasoned JS dev I find myself constantly suprised at shallow
I somewhat confused because I've read that everything should be possible at IRQL_PASSIVE, but
I was somewhat surprised to see the below work coming from a C++ background.
Somewhat stuck trying to find a newer/better SCC system for my employer. My personal
Somewhat new to php, I did some searching but didn't find a clear answer.
We have a somewhat unusual c app in that it is a database of
Being somewhat lazy about implementing lots of IEqualityComparers , and given that I couldn't
I have a somewhat complex SQL query that is pulling from a table of

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.