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

Virtual Superclasses Should Have Virtual Destructors #153

Closed
wimos-ai opened this issue May 31, 2024 · 0 comments
Closed

Virtual Superclasses Should Have Virtual Destructors #153

wimos-ai opened this issue May 31, 2024 · 0 comments

Comments

@wimos-ai
Copy link
Contributor

Classes like f2c::obj::BaseObjective have virtual methods but no virtual destructors.

For example, this function from fields2cover.cpp

std::unique_ptr<obj::SGObjective> getSGObjPtr(const Options& opt) {
  std::unique_ptr<obj::SGObjective> ptr;
  switch (opt.sg_obj) {
    case SGObjFunc::N_SWATH_MOD:
      ptr = std::make_unique<obj::NSwathModified>();
      break;
    case SGObjFunc::N_SWATH:
      ptr = std::make_unique<obj::NSwath>();
      break;
    case SGObjFunc::SWATH_LENGTH:
      ptr = std::make_unique<obj::SwathLength>();
      break;
    case SGObjFunc::FIELD_COV:
      ptr = std::make_unique<obj::SwathLength>();
      break;
    case SGObjFunc::OVERLAPS:
      ptr = std::make_unique<obj::Overlaps>();
      break;
  }
  return ptr;
}

When the returned unique pointer is destroyed, it calls the SGObjective destructor, and not any of the obj::NSwathModified, obj::NSwath, obj::SwathLength, obj::SwathLength, or obj::Overlaps destructors.
This is undefined behavior.

Gonzalo-Mier added a commit that referenced this issue Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant