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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:26:45+00:00 2026-05-30T01:26:45+00:00

I have a question similar to How to manage object life time using Boost

  • 0

I have a question similar to How to manage object life time using Boost library smart pointers? but, in my case, the “object” isn’t a C++ object at all, but an opaque type returned/passed out from a C API. The type does not have pointer semantics, i.e., there is no dereferencing; it is, however, passed as an argument to other functions in the C API. The type also has a definitive close API which must be called in order to clean up internal resources.

So, I have a C API that’s something along the lines of

opaque_legacy_type_t x;
XXopen(..., &x); // allocates/opens resource and fills out 'x' to be used later
XXdoSomethingWithResource(x, ...); // do something with resources related to 'x'
...more actions...
XXclose(x); // closes and cleans up resources related to 'x'

For various reasons, in my C++ code I would like to manage “instances” of opaque_legacy_type_t much like I would manage heap-allocated object instances, i.e. with similar sharing semantics as boost::shared_ptr<>. It seems that shared_ptr offers enough that I can manage calling XXclose by doing this:

opaque_legacy_type_t x;
XXopen(..., &x);
boost::shared_ptr<opaque_legacy_type_t> managed(x, XXclose);

But, since opaque_legacy_type_t doesn’t have pointer semantics, the usage of managed is a bit clumsy.

What I’d like to do is have something like a managed_type that is similar to shared_ptr, and am looking for ideas that don’t require me to write it all.

EDIT: I corrected my original screw-up in the example. The legacy API takes the opaque type by value rather than by pointer.

  • 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-30T01:26:47+00:00Added an answer on May 30, 2026 at 1:26 am

    You could use boost smart pointers together with the pimpl idom:

    class shared_opaque_legacy_type_t {
        struct impl {
            opaque_legacy_type_t t;
            impl(...) { XXOpen(..., t); }
            ~impl(...) { XXClose(t); }
        }
        boost::shared_ptr<impl> _impl;
    public:
        shared_opaque_lagacy_type_t(...) : _impl(new impl(...)) {}
    
        opaque_legacy_type_t* get() {
            return _impl->t;
        }
    };
    
    
    shared_opaque_legacy_type_t x(...);
    XXdoSomethingWithResource(x.get(), ...);
    

    The drawback is that you could still call XXclose(x.get()) and invalidate your object.

    UPDATE: Fixed it. 🙂

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

Sidebar

Related Questions

I have asked a question similar to this in the past but this is
I have a similar question than django cross-site reverse . But i think I
I have asked a similar question before, but I didn't have a firm grasp
Sorry I know similar question have been asked many times before but like most
I have a question similar to the one here: Event handlers inside a Javascript
I have a question involving the Replace Method. I saw a question similar to
I have a similar question to this one SQL products/productsales I want to do
I have asked similar question for Linux RPM ( Adding License Agreement in RPM
I have a similar question as here: Is it possible to use ContentProvider and
The only similar question I have found is: Insert php boolean into mysql bit

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.