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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:17:35+00:00 2026-05-28T04:17:35+00:00

My question is building on this question: Correct way to inherit from a virtual

  • 0

My question is building on this question: Correct way to inherit from a virtual class with non-virtual parent.

Is my understanding right that in the case which is described in the question, the Three and Two part of new allocated object are leaking because they are not destructed?

Source:

#include <iostream>

struct One
{
    ~One() {
        std::cout << "~One()\n";
    }
};

struct Two : One
{
    virtual ~Two() {
        std::cout << "~Two()\n";
    }

    virtual void test() = 0;
};

struct Three : Two
{
    virtual ~Three() {
        std::cout << "~Three()\n";
    }

    virtual void test() {
        std::cout << "Three::test()\n";
    }
};

int main()
{
    Two* two = new Three;
    two->test();

    One* one = two;
    delete one;
}
  • 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-28T04:17:36+00:00Added an answer on May 28, 2026 at 4:17 am

    Yes, that’s correct. The definition of a memory leak is a situation where you are unable to delete something that you created (and whose lifetime you are therefore responsible for managing).

    As the answers to that question indicate, delete one invokes undefined behavior (which in most cases will probably translate to a regular old memory leak, but things could be as bad as nasal demons) because the runtime type of the specified object does not match its static (declared) type, and the static type does not have a virtual destructor.

    The applicable section from the C++ standard is this one:

    §5.3.5/3: In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined.

    The solution is either to declare all of the destructors virtual, or not to delete the objects through a pointer to One.

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

Sidebar

Related Questions

Sorry for such a trivial question, but I'd feel silly building this if it
Unlike this question: Linker Error while building application using Boost Asio in Visual Studio
I'm building a report of case results with a parent-child grouping on the row
Rails Newb here asking his first question.... I'm building a personal blog-style website that
I'm building a question / answer wizard that uses xsl to generate the html
This question can have different answers because everyone would do it his own way.
I am building an online survey tool. A Question entity looks like this: [Question]
This is a multi part question. Background: I'm building my first site using CakePHP,
I'm building a quiz web application that is dynamically generated from a database on
I am building a simple class to hold related methods. Part of this code

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.