Interfaced object set to nil (lint rule)
Lint rule, OFF by default: an object reference to a TInterfacedObject descendant is set to nil while it holds a fresh instance, which leaks it.
Read this to see when X := nil leaks a TInterfacedObject descendant and when it only clears an alias. Works per file; an index widens the class-descent check beyond the file.
How to run it
drag-lint lint-all [--profile <name>] [--target-compiler <ver|auto>] [--db <file.sqlite>] [--project <.dproj>] [--build-config <name>] [--rule <id>[,<id>...]] [--disable id,...] [--output <report.txt>] [--json] [--labels] [--quiet] [--lint-third-party] [--no-preprocess] [--shared-db <db> ...] [--library-db none] [--no-budget] [--changed-since <rev>]
Example:
drag-lint lint-all --db <project.sqlite> --rule interfaced-object-nil-leak
Full flag list: lint-all in the CLI reference.
Where to click
No IDE surface: this feature runs from the CLI (see How to run it).
Details
An OBJECT reference to a TInterfacedObject descendant is set to nil while it holds a fresh instance. Only an interface reference counts; an object reference holds no count, so nil releases nothing and the instance leaks. OFF by default.
function Build: TThing; // TThing = class(TInterfacedObject, IThing)
begin
try
Result := TThing.Create;
Result.Load;
except
Result := nil; // finding: the instance leaks. Return IThing, or Free it.
end;
end;
Running it
drag-lint lint <file> --rule interfaced-object-nil-leak
drag-lint lint-all --db <project.sqlite> --rule interfaced-object-nil-leak
What stays silent
Result := nilwith no instance created before it (the find idiom).- A reference that may have reached an interface holder first: a bare use (
I := T,Sink(T)) or a method call on it (T.Subscribecan hand over Self). In those cases thenilonly clears an alias. - A variable typed as an interface.
T.Freebefore thenil.if C then X := T.Create else X := nil: the two are on different branches.- A class whose chain to
TInterfacedObjectis unknown. The rule reads the same file, then the index. - A plain class. That is a different leak, and not what this rule is about.