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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:34:07+00:00 2026-05-24T01:34:07+00:00

Generally I follow the Google style guide, which I feel aligns nicely with the

  • 0

Generally I follow the Google style guide, which I feel aligns nicely with the way I see things. I also, almost exclusively, use boost::scoped_ptr so that only a single manager has ownership of a particular object. I then pass around naked pointers, the idea being that my projects are structured such that the managers of said objects are always destroyed after the objects that use them are destroyed.

http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Smart_Pointers

This is all great, however I was just bitten by a nasty little memory stomp bug where the owner just so happened to be deleted before objects that were using it were deleted.

Now, before everyone jumps up and down that I’m a fool for this pattern, why don’t I just use shared_ptr ? etc., consider the point that I don’t want to have undefined owner semantics. Although shared_ptr would have caught this particular case, it sends the wrong message to users of the system. It says, “I don’t know who owns this, it could be you!”

What would have helped me would have been a weak pointer to a scoped pointer. In effect, a scoped pointer that has a list of weak references, that are nulled out when the scoped pointer destructs. This would allow single ownership semantics, but give the using objects a chance to catch the issue I ran into.

So at the expense of an extra ‘weak_refs’ pointer for the scoped_ptr and an extra pointer for the ‘next_weak_ptr’ in the weak_ptr, it would make a neat little single owner, multiple user structure.

It could maybe even just be a debug feature, so in ‘release’ the whole system just turns back into a normally sized scoped_ptr and a standard single pointer for the weak reference.

So….. my QUESTIONS after all of this are:

  1. Is there such a pointer/patten already in stl/boost that I’m
    missing, or should I just roll my own?
  2. Is there a better way, that
    still meets my single ownership goal?

Cheers,
Shane

  • 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-24T01:34:08+00:00Added an answer on May 24, 2026 at 1:34 am

     2. Is there a better way, that still meets my single ownership goal?

    Do use a shared_ptr, but as a class member so that it’s part of the invariant of that class and the public interface only exposes a way to obtain a weak_ptr.

    Of course, pathological code can then retain their own shared_ptr from that weak_ptr for as long as they want. I don’t recommend trying to protect against Machiavelli here, only against Murphy (using Sutter’s words). On the other hand, if your use case is asynchronous, then the fact that locking a weak_ptr returns a shared_ptr may be a feature!

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

Sidebar

Related Questions

I find myself in conflict, regarding which code style I should follow when using
What principles do you generally follow when doing class design?
Is there a specific pattern that developers generally follow? I never really gave it
If you call LoadLibrary without a path (e.g., LoadLibrary(whatever.dll) , Windows will generally follow
I have a client/server style application which communicates using WCF which all works great.
I'm programming C++ using the underscore naming style (as opposed to camel case) which
I'm using Objectify with my first serious Google App Engine project, and generally it's
General Follow the same standards for all tests. Be clear about what each test
Is there a general rule of thumb to follow when storing web application data
What guidelines do you follow to improve the general quality of your code? Many

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.