2

I just noticed this:

function DataTransfer(...parameters...) : boolean;
var
   LTestQuery, LUpdateQuery: TFDQuery;
begin

Right at the start of the procedure:

In 32-bit (Delphi Alexandria 11.1) local variables `` etc. are nil
In 64-bit (Delphi Alexandria 11.3**) they are not. Assigned() returns true, var = nil returns false, the object inspector says 'Inaccessible value'.

Is that really a change when moving from 32 to 64 bit?

That would be a real PITA, because I see many routines in our 10+ MLOC relying on local objects being initialized to nil.
I bumped into this when one routine gave an AV on FreeAndNil(LTestQuery) with LTestQuery being one of those uninitialized TFDQuerys.

** Currently compiling with debug dcus because I'm tracing these kinds of issues

7
  • 1
    Don't you get compiler warnings for those uninitialized varriables?
    – Pieter B
    Jun 7 at 10:33
  • @PieterB No. That would help a lot.
    – Jan Doggen
    Jun 7 at 11:24
  • 1
    @JanDoggen: Normally you do get these warnings. Have you disabled them in project settings? Jun 7 at 11:28
  • @JanDoggen: Ah, I see that the FreeAndNil doesn't do this with its const ref approach. Jun 7 at 11:34
  • 1
    @IvanYuzafatau: We know. And this applies not only to 11.1, but to every Delphi version ever made. Also, there's no need for the "by default" part. Jun 7 at 16:46

2 Answers 2

10

In 32-bit (Delphi Alexandria 11.1) local variables `` etc. are nil

I'm afraid this is very, very wrong.

Local variables of unmanaged types (which includes ordinal types, pointers, and objects) have NEVER been initialised (to 0, nil).

If you have been relying on this, you have had a bug. Sure, they MAY be 0, nil etc. initially every now and then, but that's only by coincidence.

So now you know why your app crashes or malfunctions every fifth day at some customers...

When you crash by an AV you are lucky. You can also corrupt your application's memory by writing to random addresses. In theory, you could change a monetary value somewhere and send an incorrect invoice a week later...

Is that really a change when moving from 32 to 64 bit?

No.

That would be a real PITA, because I see many routines in our 10+ MLOC relying on local objects being initialized to nil.

That's tough. But really there's no alternative to fixing your bugs.

On the bright side, you should be thankful your company hasn't been producing software for pacemakers or nuclear missiles!

5
  • The funny and strange thing is our apps do not "crash or malfunctions every fifth day at some customers.." And the only thing I have seen in the 32-bit past is 'properly' nil-initialized local objects. Well, let's see it as an opportunity for cleaning up the code then. I noticed that the 64-bit compiler and runtime are more sensitive/strict, which is a good thing.
    – Jan Doggen
    Jun 7 at 10:13
  • @JanDoggen: The initial value of a non-managed local variable is whatever happens to be at that place in the computer's RAM at that time. If you have a lot of RAM, there's a good chance you get zeros simply because a lot of the bytes in your memory are zeros. And even in memory that is actually used, zeros are common. For instance, in a 32-bit integer value, you often see zeros because integers like 46 are more common than 2147483642 (say). And in English-language Unicode strings, every second byte is zero. And you have padding, empty buffers, etc. Jun 7 at 10:15
  • "And the only thing I have seen in the 32-bit past is 'properly' nil-initialized local objects." no, you've never seen that Jun 7 at 17:47
  • "When you crash by an AV you are lucky" - I would give you a +1 only for this statement (but I already gave you a +1 for your general answer). Not all programmers understand how lucky we are (and thankful should be) when we get an AV! I always stated that AVs are our friends, not our enemies!
    – Gabriel
    Aug 10 at 12:53
  • "your company hasn't been producing software for pacemakers or nuclear missiles!" Another one that deserves a +1! I have been until recently in a project where people turned off compiler warnings and there was a lot of code relying on uninitialized variables. It is just impressive, though, how often these variables are set (by pure luck) to zero!
    – Gabriel
    Aug 10 at 12:54
1

Local variables and objects are not initialized as a W1036 should have made you aware of. That you did not get the warning looks to be a bug.

Delphi seems to be acting odd with W1036 Variable '%s' might not have been initialized warnings if there is a FreeAndNil() latter on. The warning is produced by the bad/buggy code below but the warning goes away if the FreeAndNil() line is uncommented which is NOT good.

procedure TForm1.Button1Click(Sender: TObject);
Var
  AObject : TObject;
begin
  IF Assigned(AObject) then button1.Caption := AObject.ClassName;
  //FreeAndNil(AObject);
end;
1
  • This is a bug indeed. You should report it to Embarcadero. Jun 8 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.