RAD Studio (Common)
ContentsIndex
PreviousUpNext
E2221: $WEAKPACKAGEUNIT '%s' cannot have initialization or finalization code (Delphi)

A unit which has been flagged with the $weakpackageunit directive cannot contain initialization or finalization code, nor can it contain global data. The reason for this is that multiple copies of the same weakly packaged units can appear in an application, and then referring to the data for that unit becomes and ambiguous proposition. This ambiguity is furthered when dynamically loaded packages are used in your applications.

(*$WEAKPACKAGEUNIT*)
unit yamadama;
interface
implementation
  var
    Title : String;

initialization
  Title := 'Tiny Calc';
finalization
end.

In the above example, there are two problems: Title is a global variable, and Title is initialized in the initialization section of the unit.  

There are only two alternatives: either remove the $weakpackageunit directive from the unit, or remove all global data, initialization and finalization code.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!