[fpc-devel] Initialize/Finalize management operators and Default intrinsic

Maciej Izak hnb.code at gmail.com
Tue Apr 12 12:01:06 CEST 2016


2016-04-12 11:21 GMT+02:00 Sven Barth <pascaldragon at googlemail.com>:

> > That might be, but this just adds a new category of possibilities for
> such bugs and that is something that we should not do, especially one as
> subtle as this.
> >
> > One possible solution would be to disallow records with management
> operators for global variables (variables created for Default() might be an
> exception here, but I'd need to think that through).
>
> Another possibility I just thought of: have the compiler analyse such
> inter unit dependencies in the initialization sections (yes, I know, easier
> said than done) and print a warning in case something might depend on the
> order of execution.
> If the user is at least aware that there might be something fishy going on
> then I'm more lenient.
>

another way is to introduce attributes for selected language elements in
Delphi compatible way. For example in Delphi we have:

  TCustomAttribute = class(TObject)
  end;
  WeakAttribute = class(TCustomAttribute);
  UnsafeAttribute = class(TCustomAttribute);
  RefAttribute = class(TCustomAttribute);
  VolatileAttribute = class(TCustomAttribute);

which is used like this:

procedure Foo([ref] const X: TFoo); // FPC equivalent is procedure
Foo(constref X: TFoo);

or like this for TObject field for ref. count for ARC:

[Volatile] FRefCount: Integer;

Note: these attributes are on my TODO list.

To protect our management operators we should declare in System.pp:

  FixedAttribute = class(TCustomAttribute); // or SystemInitializedAttribute

Now the gSomething from example is declared as:

var
  [Fixed] gSomething: TSomeType; // fixed can be used only for global
variables and for class var/{$J+} const (aka static var)

... that means "don't' [Initialize|Finalize|FillChar|Whatever] this
variable from module but from System.pp (in special table in initialization
section)". Any module may contain special hidden section (if [Fixed]
attribute is detected); that will register all global variables declared
with [Fixed] and any variable with [Fixed] will be excluded from standard
initialization/finalization process. That special all "Fixed Sections" from
all modules should be called before any other initialize/finalize to
register all variables (and before system initialize section ofc, where all
registered fixed variables shall be initialized, and after all ofc
finalized).

That approach is also usefully to fix existing problems with
initialize/finalize (!)

-- 
Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160412/011171be/attachment.html>


More information about the fpc-devel mailing list