org.sonar.l10n.delphi.rules.community-delphi.InstanceInvokedConstructor.html Maven / Gradle / Ivy
The newest version!
Why is this an issue?
Delphi constructors should only be called on the class. A common error for new Delphi programmers
is to call a constructor on an instance variable instead. This does not create a new instance of
the object, instead being treated as a regular routine invocation.
If the instance has already been initialized, it will unexpectedly modify it by re-running the
constructor.
If the instance is uninitialized, an access violation may occur (the constructor will attempt
to set fields on uninitialized memory).
How to fix it
Call the constructor on the class instead:
procedure Example;
var
Foo: TFoo;
begin
Foo.Create;
end;
procedure Example;
var
Foo: TFoo;
begin
Foo := TFoo.Create;
end;
© 2015 - 2024 Weber Informatics LLC | Privacy Policy