Safeguard
Application Security•

CWE-1045 Virtual Destructor Missing in a Child Class

When a parent has a virtual destructor and a child does not, cleanup can be skipped and memory can leak. CWE-1045 explained with a C++ sketch.

Safeguard Security Team
2 min read

CWE-1045 is an object oriented weakness: a parent class has a virtual destructor, but one of its child classes does not. MITRE lists it as a Base weakness without a specific language, though the idea is most familiar from C++.

How it happens

A virtual destructor makes sure that deleting an object through a base class pointer runs the destructor of the actual derived type. If a child class breaks that chain, the child's cleanup might not run.

class Base {
public:
    virtual ~Base() {}
};

class Child : public Base {
    char *buffer;
public:
    Child() : buffer(new char[1024]) {}
    // Child's own cleanup is missing or not declared as intended
};

MITRE's consequence is reduced reliability. The child might not perform essential destruction operations. If the relevant code is reachable by an attacker, the reliability problem might introduce a vulnerability, such as a memory leak, which MITRE ties to CWE-401. No CVEs are observed for this entry.

How to find it

MITRE lists no detection methods. Compiler warnings about non virtual destructors in polymorphic classes, and static analysis, will flag many cases. Review class hierarchies where objects are deleted through base pointers.

How to fix it

MITRE lists no formal mitigations. Reasonable steps:

  1. Declare destructors virtual in every class of a polymorphic hierarchy.
  2. Release owned resources in the destructor, or use smart pointers and containers that do it for you.
  3. Turn on compiler warnings for this pattern and treat them as errors in CI.
  4. Test long running paths for memory growth.

Quick check

Why is a memory leak a security concern?

If an attacker can trigger the code repeatedly, the leak can exhaust memory and cause denial of service.

Sources

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.