Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing virtual destructor in base class CubicalComplex with public inheritance #1458

Open
rolanddenis opened this issue Jan 31, 2020 · 0 comments
Labels

Comments

@rolanddenis
Copy link
Member

VoxelComplex publicly inheritates from CombicalComplex but the later class is not designed for this: its destructor should be virtual so that to guarantee that a VoxelComplex object is correctly destroyed in all cases. In particular, in case of polymorphism, the destructor of the attributes of VoxelComplex will not be called thus leading to potential memory leak (attributes are CountedPtrOrPtr).

Simple example:

#include <iostream>

struct CountedPtrOrPtr
{
    CountedPtrOrPtr() { std::cout << "CountedPtrOrPtr" << std::endl; }
    ~CountedPtrOrPtr() { std::cout << "~CountedPtrOrPtr" << std::endl; }
};

struct CubicalComplex
{
    CubicalComplex() { std::cout << "CubicalComplex" << std::endl; }
    ~CubicalComplex() { std::cout << "~CubicalComplex" << std::endl; }
};

struct VoxelComplex : CubicalComplex
{
    CountedPtrOrPtr ptr;
    VoxelComplex() { std::cout << "VoxelComplex" << std::endl; }
    ~VoxelComplex() { std::cout << "~VoxelComplex" << std::endl; }
};

int main()
{
    CubicalComplex* cc = new VoxelComplex();
    delete cc;
    return 0;
}

that outputs:

CubicalComplex
CountedPtrOrPtr
VoxelComplex
~CubicalComplex

but with a virtual CubicalComplex destuctor:

CubicalComplex
CountedPtrOrPtr
VoxelComplex
~VoxelComplex
~CountedPtrOrPtr
~CubicalComplex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant