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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:33:41+00:00 2026-05-10T23:33:41+00:00

I have a factory that builds the objects with longest lifetime in my application.

  • 0

I have a factory that builds the objects with longest lifetime in my application. These have types, lets say, ClientA and ClientB, which depend on Provider (an abstract class with many possible implementations), so both clients have a reference to Provider as member.

According to the command-line arguments, the factory chooses one implementation of Provider, constructs it (with ‘new‘), and passes it to the constructors of both clients.

The factory returns an object that represents my entire app. My main function is basically this:

int main(int argc, char** argv) {     AppFactory factory(argc, argv);     App app = factory.buildApp();     return app.run(); } 

And the buildApp method is basically this:

App AppFactory::buildApp() {     Provider* provider = NULL;      if (some condition)     {         provider = new ProviderX();      }     else     {         provider = new ProviderY();     }      ClientA clientA(*provider);     ClientB clientB(*provider);      App app(clientA, clientB);     return app; } 

So, when execution ends, destructors of all objects are called, except for the provider object (because it was constructed with ‘new‘).

How can I improve this design to make sure that the destructor of the provider is called?

EDIT: To clarify, my intention is that both clients, the provider and the App object to share the same lifetime. After all answers, I now think both clients and the provider should be allocated on the heap its references passed to the App object, which will be responsible for deleting them when it dies. What do you say?

  • 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. 2026-05-10T23:33:41+00:00Added an answer on May 10, 2026 at 11:33 pm

    It’s very simple with a shared ownership smart pointer:

    App AppFactory::buildApp() {     boost::shared_ptr<Provider> provider;      if (some condition)     {         provider.reset(new ProviderX());      }     else     {         provider.reset(new ProviderY());     }      ClientA clientA(provider);     ClientB clientB(provider);      App app(clientA, clientB);     return app; } 

    Assuming the app object owns the clients, and the clients all share the one provider. Make the clients take a shared_ptr<Provider> then, instead of a Provider& . As long as there is still a copy of a shared_ptr owning the provider object, the object won’t be freed.

    The best would be to not copy clientA and clientB, and not copy app by returning it by value, but move the clients into the app, and move the app itself into the returned object. That will be possible with the upcoming C++ version. But currently, either you make them pointers (using shared_ptr), or you keep copying them. Another option would be to use auto_ptr, which has a pseudo-transfer-of-ownership semantic. But that template has some inherent problems. So you should avoid using it.

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

Sidebar

Related Questions

I have a factory class, DocumentLoaderFactory , which simply returns an instance that implements
In my console application have an abstract Factory class Listener which contains code for
I have a factory that creates objects of class MyClass , returning already generated
With generics on languages like C# or Java, you can have a factory that
I have a factory method that generates django form classes like so: def get_indicator_form(indicator,
I have a script with a factory method that I would like to return
In classic ASP (which I am forced to use), I have a few factory
I have a model Foo that has_many 'Bar'. I have a factory_girl factory for
Let's say I have a business object that is very expensive to instantiate, and
For the sake of simplicity say I have multiple Xtext DSLs that all describe

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.