-3

My company has been using Delphi for decades, and our core program was made in a quite old version. It has about 1.3 million lines of code.

After upgrading to Delphi 10 Rio, a major problem occured. Where our local function variables used to be initialized with a default value (integer would be 0, boolean would be false), it seems they no longer are. Now all my variables get a random value as they are created, so an integer gets something like 408796 as it's value.

I suppose this isn't an issue with new development, but I am sure you can see the problem in our large code base. We have never manually assigned default values to these variables as it worked fine. Object variables, however, have always had this problem. All properties get random values, so we have adressed that as we went. But now our program completely breaks, as all counters etc. are starting at high values instead of 0. And running through the entire project to fix this would take months.

Is there perhaps a compiler option to change this? Seems very backwards that they would have changed this intentionally, as it would be pretty stupid. Why remove functionality that all developers expect? I had actually expected it to go the other way, that object variables would no longer need manual default values, and that they maybe implemented a garbage collector. But is seems Delphi has seriously regressed?

If there is some option to fix this, PLEASE let me know.

  • 1
    Heap variables are still initialized; that's something that has been around for ages. It would be better if you were more specific about the issues you're having, since without the detail, everything is pure guesswork. – Dave Nottage Sep 20 '19 at 09:27
  • 1
    From which version did you upgrade? You should actually provide detailed examples of code that you are having trouble with. Please do so. – Tom Brunberg Sep 20 '19 at 09:44
  • 1
    See [Are delphi variables initialized with a value by default?](https://stackoverflow.com/a/132770/576719) – LU RD Sep 20 '19 at 09:50
  • 1
    "Where our local function variables used to be initialized with a default value (integer would be 0, boolean would be false), it seems they no longer are." No, local integer and boolean variables have _never_ been initialized. – Andreas Rejbrand Sep 20 '19 at 10:05
  • 3
    I'm positive that you've misdiagnosed the problem. It's simply not possible for a sizeable application to survive all those years without initializing local variables. – Sertac Akyuz Sep 20 '19 at 14:02
  • @SertacAkyuz Indeed, it's at the mercy of whatever memory just happens to already be sitting at those addresses at the time... – Jerry Dodge Sep 20 '19 at 23:40

1 Answers1

8

Local variables of unmanaged types such as integer and boolean have never been initialized automatically. If you have code that relies on that, that code has always been broken. If your program has not been affected then that has been down to chance rather than planning. Had you enabled compiler hints and warnings, the compiler would have told you this.

All variables of managed types are default initialized. All global variables are default initialized. All class member fields are default initialized. It is just local variables of unmanaged types, and memory allocated dynamically with GetMem that is not default initialized.

Your only path forward is to fix your code to make sure that variables are initialized. Use the compiler hints and warnings to help you.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490