Jump to content
chkaufmann

Class Constructor in Delphi 10.4

Recommended Posts

In 10.3 (Rio), class constructors are implemented the same way as initialization sections of units. This means, I have no control about the order. So if I create an object of my class in an initialization section, I cannot be sure, that the class constructor for this class was already called.

 

Just wondering: is this problem fixed in 10.4? Or is it still done the same way?

 

Christian

Share this post


Link to post
28 minutes ago, chkaufmann said:

So if I create an object of my class in an initialization section, I cannot be sure, that the class constructor for this class was already called 

According to what Allen wrote in 2009 that is not true.

 

If there is a bug with that please link to the QP entry and/or code to repro wrong behavior.

Edited by Stefan Glienke
  • Like 2

Share this post


Link to post

Class constructors are executed in the lexical order in which they implemented (not declared). Class destructor executed in the  reverse order in which they implemented. 

NOTE: Many languages follow the same principle.

  • Like 2

Share this post


Link to post
1 hour ago, Mahdi Safsafi said:

NOTE: Many languages follow the same principle.

Those languages to my knowledge don't also have something like the initialization part of a unit which might cause a chicken-egg-problem.

  • Like 1

Share this post


Link to post
31 minutes ago, Stefan Glienke said:

Those languages to my knowledge don't also have something like the initialization part of a unit which might cause a chicken-egg-problem.

In my experience, the initialization section is dangerous. When you have a large legacy project with many unit dependency cycles, and begin working to clean up that issue, the changes in uses clauses may lead to reordering of the initialization sequence. I found it necessary to log the existing order, then replace initialization sections with InitializeUnit procedures called from an initialization manager. That let me proceed with the cleanup. Without that change, I soon had an app with a lower dependency cycle count, but which did not work.

 

  • Like 1

Share this post


Link to post
32 minutes ago, Stefan Glienke said:

Those languages to my knowledge don't also have something like the initialization part of a unit which might cause a chicken-egg-problem.

You're absolutely right Stefan.

Particularly, the awesome D language has initialization and finalization sections, but it implements them in a very sexy way :

  1. Static constructors(initialization) are executed to initialize a module(unit)'s state. Static destructors(finalization) terminate a module's state.
  2. A module may have multiple static constructors and static destructors. The static constructors are run in lexical order, the static destructors are run in reverse lexical order.
  3. Non-shared static constructors and destructors are run whenever threads are created or destroyed, including the main thread.
  4. Shared static constructors are run once before main() is called. Shared static destructors are run after the main() function returns.

But as you said, it can be a source of lot of bugs.

  • Like 1

Share this post


Link to post
1 hour ago, Mahdi Safsafi said:

the awesome D language

Isn't that just a subset of Delphi? :classic_cool:

  • Haha 1

Share this post


Link to post

D is a truly wonderful language, and Andrei Alexandrescu's book is by some distance the best programming book I have ever read.

  • Like 2

Share this post


Link to post
1 hour ago, Uwe Raabe said:

Isn't that just a subset of Delphi? :classic_cool:

If it was true ... then we're definitely using Elphi 😉

  • Haha 3

Share this post


Link to post
1 hour ago, David Heffernan said:

D is a truly wonderful language, and Andrei Alexandrescu's book is by some distance the best programming book I have ever read.

Indeed ! D official doc is also super good. Perhaps Andrei was behind it too.

  • Like 1

Share this post


Link to post
5 hours ago, Stefan Glienke said:

If there is a bug with that please link to the QP entry and/or code to repro wrong behavior.

The problem is to create a reproducable case. The order of initialization sections and class constructor is at least partial random. So things work fine but if you add a unit or change one of the uses in a project, the order is different. So I don't use class constructor anymore.

Share this post


Link to post
2 hours ago, chkaufmann said:

The problem is to create a reproducable case. The order of initialization sections and class constructor is at least partial random. So things work fine but if you add a unit or change one of the uses in a project, the order is different. So I don't use class constructor anymore.

Sounds more like you have some serious issue with your globals

Share this post


Link to post
5 hours ago, chkaufmann said:

The order of initialization sections and class constructor is at least partial random.

No. It is deterministic. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×