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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:34:45+00:00 2026-06-11T19:34:45+00:00

When answering another question, it occurred to me that I maybe optimize some of

  • 0

When answering another question, it occurred to me that I maybe optimize some of my own older code that has err.. ‘less than optimal’ lifetime management.

I have at least one app where access/lifetime an object is controlled with a shared_ptr. This ptr is dynamically allocated so that it can be ‘atomically’ swapped out for another *shared_ptr, (and hence an updated object managed by the new ptr), without any locking. This seems to work fine, but I deliberately leak the old ptr because I don’t know when the last thread is going to be finished with it.

It occured to me now that I could, (maybe), delete() the old *shared_ptr in the dtor of the old object being managed. I would load the *sharedPtr into a private data member of the managed object upon creation so that the dtor could delete it.

Has anyone done this, or have any views on why it might be unsafe? I could try it, but I’m worried that, like so many multithreaded ‘optimizations’ it might just ‘appear to work’ until after I delivered it 🙁

  • 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-06-11T19:34:46+00:00Added an answer on June 11, 2026 at 7:34 pm

    It sounds like you have a bad design issue in your code more than anything else. First off, why would you want to create a shared_ptr*? That already seems wrong.

    Then leaking it because you don’t know when other threads will be finished with it? What?? That’s bad.

    Why not just have two shared_ptr and use them properly? Maybe that will make your life much easier.

    Also, NO, you cannot get the shared_ptr* to delete itself with the destructor of the object it owns. That would probably get into an unbreakable cycle. Because the shared_ptr is trying to delete the object it owns, and then that will try to delete the owner, which in turn…. You get the idea… That is silly.

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

Sidebar

Related Questions

When answering another question, I realized that the following program does not quite do
In answering another persons question here on SO, I discovered that there is a
As part of answering another question, I came across a piece of code like
As part of answering another question, I wanted to show that the insane level
As part of answering another question, I wrote the following code whose behaviour seems
I was answering another question about creating a binding in code-behind, and my original
In answering another question someone pointed out that in C# you can access a
Answering another question about how const string data was stored in an executable a
In answering another question, someone showed me the following tutorial, in which the author
In answering this code golf question , I ran across a problem in my

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.