• Home
  • Welcome to the Jungle
  • Elements of Modern C++ Style
  • About

Sutter’s Mill

Herb Sutter on software, hardware, and concurrency

Feeds:
Posts
Comments
« Facebook Folly – OSS C++ Libraries
GotW #105: Smart Pointers, Part 3 (Difficulty: 7/10) »

GotW #104: Solution

2012-06-05 by Herb Sutter

The solution to GotW #104 is now live.

Posted in C++, GotW | 6 Comments

6 Responses

  1. on 2012-06-05 at 12:27 pm Motti Lanzkron

    There is another option, that the factory maintains some kind of pool of objects and that an object (once created) is never destroyed. This is probably a very rare case but it does happen (e.g. Singleton design pattern). In this case the factory is correctly written and shouldn’t be changed.

    What’s missing from the standard is some kind of unowned_ptr<T> which would document these cases.


  2. on 2012-06-05 at 5:15 pm Joe Gottman

    This case is handled by a shared_ptr with a user-defined deleter that does nothing.

    struct null_deleter
    {
    void operator()(const void *) { /* Do nothing */ }
    };


  3. on 2012-06-06 at 1:20 am MF

    I think shared_ptr is rather suspicious since it breaks the lifetime semantics of shared_ptr.

    Normally having a shared_ptr implies that the pointed to object will be kept alive until all shared_ptr’s to it are gone but if you use a null_deleter then you can end up with a valid shared_ptr that points to a deleted object.


  4. on 2012-06-06 at 1:22 am MF

    The suspicious thing is obviously shared_ptr<T, null_deleter>, not shared_ptr in general.


  5. on 2012-06-06 at 8:20 am Adrian

    There’s another ripple that the solution doesn’t acknowledge.

    The original calling code has a raw pointer. After all, it’s modern and so it’s using `auto` when it calls the factory function that returns a raw pointer. Therefore, assuming the current calling code it correct, it’s making at least one cleanup call (e.g., `delete widget;` or `free_widget(widget);`). Changing the factory to a smart pointer means you’ll have to search through all the calling code to delete those cleanup operations.

    I would have commented directly on the solution, but it seems you disable comments on the solutions.


  6. on 2012-06-06 at 1:43 pm Herb Sutter

    @Adrian: The compiler will tell the caller all the places that delete so that one’s fine. Just like it will tell you all the cases where the caller might try to convert to store the pointer as another type.



Comments are closed.

  • Tweets

    • GotW #5: Overriding Virtual Functions: Virtual functions are a pretty basic feature, but they occasionally har... bit.ly/14oTLHx 1 day ago
    • GotW #4: Class Mechanics: How good are you at the details of writing classes? This item focuses not only on bl... bit.ly/16Fqug8 1 day ago
    • GotW #4: Class Mechanics (7/10): How good are you at the details of writing classes? This item focuses not onl... bit.ly/10TmyVQ 5 days ago
    Follow @herbsutter
  • Popular

    • GotW #4 Solution: Class Mechanics
    • GotW #5: Overriding Virtual Functions
    • Elements of Modern C++ Style
  • Categories

    • Apple
    • C# / .NET
    • C++
    • Cloud
    • Concurrency
    • Effective Concurrency
    • Friday Thoughts
    • GotW
    • Hardware
    • Java
    • Microsoft
    • Opinion & Editorial
    • Reader Q&A
    • Software Development
    • Talks & Events
    • Uncategorized
    • Web

Blog at WordPress.com.

Theme: Customized MistyLook by WPThemes.


Follow

Get every new post delivered to your Inbox.

Join 1,409 other followers

Powered by WordPress.com