View Issue Details

IDProjectCategoryView StatusLast Update
0036623FPCCompilerpublic2020-02-15 22:01
ReporterOndrej Pokorny Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Product Version3.3.1 
Summary0036623: Compiler always call class constructors even if classes are not used
DescriptionUsage of the "Self" class in a class constructor has to be ignored by the smart linking mechanism so that classes that are not used anywhere else are not linked into the executable.

http://docwiki.embarcadero.com/RADStudio/Rio/en/Methods_(Delphi)#Class_Constructors

This is also logical - that is exactly the difference between the initialization section and class constructors. If this difference is not present, the biggest advantage/difference compared to the initialization section of class constructors is lost.

---
I know this Delphi incompatiblity is documented: https://www.freepascal.org/docs-html/current/ref/refsu29.html#x80-1020006.5.5 but anyway IMO it cannot be really justified:
1.) If you always want to add the class to the EXE, use the class in the initialization section.
2.) If you need to call initialization code and you want to skip the class from the EXE if it isn't used elsewhere, you can use class constructors in Delphi. In FPC you don't have this possibility.
Steps To ReproduceThe following program writes "bang" when compiled with FPC but does not write anything in Delphi.

program ClassConstructors;
{$mode delphi}
type
  TMyClass = class(TObject)
  public
    class constructor Create;
  end;
{ TMyClass }
class constructor TMyClass.Create;
begin
  TMyClass.Create.Free;
  Writeln('bang');
end;
begin
end.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget-
Attached Files

Relationships

related to 0029334 new Incorrect order of class constructors calls 
has duplicate 0036625 resolvedMichael Van Canneyt class constructors and initialization sections are executed even if main program is empty 

Activities

Florian

2020-02-15 22:01

administrator   ~0121114

For the record: I see little chances to get this ever fixed in general as FPC depends on the smart linking capabilities of the external linkers.

Issue History

Date Modified Username Field Change
2020-01-24 09:23 Ondrej Pokorny New Issue
2020-01-24 09:23 Ondrej Pokorny Relationship added related to 0029334
2020-01-24 09:43 Ondrej Pokorny Description Updated View Revisions
2020-01-24 09:43 Ondrej Pokorny FPCTarget => -
2020-01-24 10:34 Michael Van Canneyt Relationship added has duplicate 0036625
2020-02-15 22:01 Florian Note Added: 0121114