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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:45:28+00:00 2026-06-13T23:45:28+00:00

I have trouble thinking outside the box with the following issue. I have a

  • 0

I have trouble thinking outside the box with the following issue. I have a class hierarchy:

[BaseClass] --> [Win32Class]

[BaseClass] --> [LinuxClass]

[BaseClass] --> [VxWorksClass]

The implementation classes will make calls to API-level functions. These are done inside pure virtual functions from the base class.

Now, when the user creates, uses and is finished with the object, he should call the Done function (from base class), which will do cleanup and resource de-allocation. Part of this cleanup is to call a number of API-level functions — these naturally are pure virtuals in the base class and implemented in the derived classes. So far so good.

Here’s the problem. If the user does not explicitly call Done there will be various memory leaks due to resources not being properly freed. So, I figured I’d make it easy and call Done from ~BaseClass() – automatic cleanup on destruction, i thought. Well, not so much. Since Done makes calls to these pure virtuals all hell breaks loose.

Any thoughts on how to re-design this to avoid this issue?

example code

class BaseClass{ 
  virtual ~BaseClass(){
    Done();
  }
  void Done(){
    // A bunch of OS-independent clean-up logic
    Cleanup();
    // some more OS-independent clean-up logic
  }
  virtual void Cleanup() = 0;
};
class Win32Class : public BaseClass{
  virtual void Cleanup(){
    // call some Win32-specific cleanup code
  }
};
class LinuxClass : public BaseClass{
  virtual void Cleanup(){
    // call some Linux-specific cleanup code
  }
};

==========================================
Here’s my solution. Use a wrapper class. Don;t call Done in the destructor of Win32Class or BaseClass

class Win32Wrapper{
public:
  Win32Class* object_;
public:
  Win32Wrapper(){
    this->object_ = new Win32Class;
  }
  ~Win32Wrapper(){
    this->object_->Done();
    delete this->object_;
  }
};

== How to Call Pure Virtuals in a destructor ==

class Base{
public:
  Base(){
  }
  virtual ~Base(){
    Done();
  }
  void Done(){
    Clean();
  }
  virtual void Clean() = 0;
};

class Derived : public Base{
public:  
  Derived(){
  }
  ~Derived(){
  }
  virtual void Clean(){
  }
};

The user thinks that the program will work, because the compiler doesn’t complain about the call to a PFV in Base::Done().

  • 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-13T23:45:30+00:00Added an answer on June 13, 2026 at 11:45 pm

    You can put the clean-up code in the derived destructors:

    struct Base
    {
        virtual ~Base() { }
    
        void CleanUp() { /* ... */ }
    };
    
    struct Derived : Base
    {
        virtual ~Derived()
        {
            CleanUp();
        }
    };
    

    If you want, you can even put calls to further virtuals into the non-virtual Base::CleanUp() function to allow for derived-specific behaviour (though by your description it sounds like you don’t need that).

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

Sidebar

Related Questions

I am currently studying recursion in school, and I have trouble thinking about methods
So I have been trouble thinking of a way to ask this question so
I have trouble understanding what the following code means (and partly, why it even
I'm having trouble thinking of a way to do the following join in MySQL.
I'm having trouble thinking up a good way to implement a file importing class,
I have a simple problem that I'm having trouble thinking around: var oldValues :
I have trouble figuring out why a simple division like this one always returns
We have trouble implementing a nice session mechanism in JSP. One of our requirement
I have trouble with strange behavior of for..in cycle in JavaScript. I have the
I have trouble doing this... You may want to get the same result as

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.