CodeQL queries 1.24
Python, unlike statically typed languages such as Java, allows complete freedom when calling methods during object destruction.
However, standard object-oriented principles apply to Python classes using deep inheritance hierarchies.
Therefore the developer has responsibility for ensuring that objects are properly cleaned up when
there are multiple
__del__ methods that need to be called.
__del__ method more than once during object destruction risks resources being released multiple
times. The relevant
__del__ method may not be designed to be called more than once.
There are a number of ways that a
__del__ method may be be called more than once.
- There may be more than one explicit call to the method in the hierarchy of
- A class using multiple inheritance directly calls the
__del__methods of its base types. One or more of those base types uses
super()to pass down the inheritance chain.
Either be careful not to explicitly call a
__del__ method more than once, or
super() throughout the inheritance hierarchy.
Alternatively refactor one or more of the classes to use composition rather than inheritance.
In the first example, explicit calls to
__del__ are used, but
SportsCar erroneously calls
This can be fixed by removing the call to
Vehicle.__del__, as shown in
In the second example, there is a mixture of explicit calls to
__del__ and calls using
To fix this example,
super() should be used throughout.