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

Class Bmi has virtual member functions, but no virtual destructor #11

Open
PhilMiller opened this issue Apr 19, 2024 · 4 comments · May be fixed by #12
Open

Class Bmi has virtual member functions, but no virtual destructor #11

PhilMiller opened this issue Apr 19, 2024 · 4 comments · May be fixed by #12
Labels

Comments

@PhilMiller
Copy link
Member

Anything inheriting from the provided Bmi class and being referred to through that abstract base class will run afoul of an inability to properly destroy itself and its members.

Cf https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dtor

@PhilMiller PhilMiller added the bug label Apr 19, 2024
@PhilMiller
Copy link
Member Author

This results in compiler warnings such as this:

In file included from /Users/phil/Code/noaa/ngen/extern/test_bmi_cpp/src/test_bmi_cpp.cpp:6:
/Users/phil/Code/noaa/ngen/extern/test_bmi_cpp/include/test_bmi_cpp.hpp: In function 'void bmi_model_destroy(TestBmiCpp*)':
/Users/phil/Code/noaa/ngen/extern/test_bmi_cpp/include/test_bmi_cpp.hpp:275:17: warning: deleting object of polymorphic class type 'TestBmiCpp' which has non-virtual destructor might cause undefined behavior [-Wdelete-non-virtual-dtor]
  275 |                 delete ptr;

@mdpiper
Copy link
Member

mdpiper commented Apr 19, 2024

@aufdenkampe Have you run into this issue in your work BMI-ing C++ models? Note that @PhilMiller provides a fix in #12.

@aufdenkampe
Copy link

@mdpiper, I think merging PR #12 from @PhilMiller makes sense to me.

Although our team still haven't gotten far enough along with our BMI C++ work to have encountered an issue with not having a virtual destructor method, from my work with embedded C++ (in https://github.com/EnviroDIY/ModularSensors), I know that adding an virtual destructor method to every class is important, to be able to clear an instantiated object from memory. The syntax that @PhilMiller suggested is exactly the way we implement destructors in our code.

@mdpiper
Copy link
Member

mdpiper commented Apr 26, 2024

@aufdenkampe Thank you for your comment on this. I'll try to get things moving in the PR.

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

Successfully merging a pull request may close this issue.

3 participants