← 返回首页
Missing call to superclass __del__ during object destruction — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Missing call to superclass __del__ during object destruction

ID: py/missing-call-to-delete Kind: problem Security severity: Severity: error Precision: high Tags: - quality - reliability - correctness - performance Query suites: - python-code-quality.qls - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Python, unlike some other object-oriented languages such as Java, allows the developer complete freedom in when and how superclass finalizers are called during object finalization. However, the developer has responsibility for ensuring that objects are properly cleaned up, and that all superclass __del__ methods are called.

Classes with a __del__ method (a finalizer) typically hold some resource such as a file handle that needs to be cleaned up. If the __del__ method of a superclass is not called during object finalization, it is likely that resources may be leaked.

A call to the __del__ method of a superclass during object initialization may be unintentionally skipped:

Recommendation

Ensure that all superclass __del__ methods are properly called. Either each base class’s finalize method should be explicitly called, or super() calls should be consistently used throughout the inheritance hierarchy.

Example

In the following example, explicit calls to __del__ are used, but SportsCar erroneously calls Vehicle.__del__. This is fixed in FixedSportsCar by calling Car.__del__.

class Vehicle(object): def __del__(self): recycle(self.base_parts) class Car(Vehicle): def __del__(self): recycle(self.car_parts) Vehicle.__del__(self) #BAD: Car.__del__ is not called. class SportsCar(Car, Vehicle): def __del__(self): recycle(self.sports_car_parts) Vehicle.__del__(self) #GOOD: Car.__del__ is called correctly. class FixedSportsCar(Car, Vehicle): def __del__(self): recycle(self.sports_car_parts) Car.__del__(self)

References