...
The class SubClass
overrides the protected
finalize
method and performs cleanup activities. Subsequently, it calls super.finalize()
to make sure its superclass is also finalized. The unsuspecting BaseClass
calls the doLogic()
method which happens to be overridden in the SubClass
. This resurrects a reference to SubClass
such that it is not only prevented from being garbage collected but also from using its finalizer to close new resources that may have been allocated by the called method. As detailed in rule MET04 MET05-J. Ensure that constructors do not call overridable methods, if the subclass's finalizer has terminated key resources, invoking its methods from the superclass might lead one to observe the object in an inconsistent state. In some cases this can result in the infamous NullPointerException
.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9c785fb85916938c-ee551225-4825421b-bbffaf43-a00698912d5e75dd45663e0c"><ac:plain-text-body><![CDATA[ | [[MITRE 2009 | AA. Bibliography#MITRE 09]] | [CWE ID 586 | http://cwe.mitre.org/data/definitions/586.html] "Explicit Call to Finalize()", [CWE ID 583 | http://cwe.mitre.org/data/definitions/583.html] "finalize() Method Declared Public" | ]]></ac:plain-text-body></ac:structured-macro> |
| CWE ID 568 "finalize() Method Without super.finalize()" |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="52ada8719467dad7-fc8dd967-40d24969-b09e99f7-86b415a421283d5d57306fac"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [finalize() | http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#finalize()] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f638e7446d5c9419-fcbb7c57-412844d1-a50b8f0f-b6ac98adc4749d0ce55f6e78"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 7, Avoid finalizers | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="12cc9dc906cfde32-2ef11090-45254d22-8f5db91c-c354b28987cab750baa54f30"><ac:plain-text-body><![CDATA[ | [[Boehm 2005 | AA. Bibliography#Boehm 05]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e87a01954ac11df1-d90ff70d-44b549fd-960694cd-5e8f214d178fc254e2a8f3ee"><ac:plain-text-body><![CDATA[ | [[Coomes 2007 | AA. Bibliography#Coomes 07]] | "Sneaky" Memory Retention | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="39d3071dc31b4f66-09ae057a-4bd24906-b807b209-2f90f1e3587800735908d20c"><ac:plain-text-body><![CDATA[ | [[Darwin 2004 | AA. Bibliography#Darwin 04]] | Section 9.5, The Finalize Method | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="dd66f414c8592a7e-73920189-4c9a4c4c-addabce5-2b1008fd7df418cb9d7c8487"><ac:plain-text-body><![CDATA[ | [[Flanagan 2005 | AA. Bibliography#Flanagan 05]] | Section 3.3, Destroying and Finalizing Objects | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="01c8149a893aab35-96a97011-4c7a4059-966486be-8ad6862f35ca4abb5437b049"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | Section 12.6, Finalization of Class Instances | ]]></ac:plain-text-body></ac:structured-macro> |
...