Re: D7 Compiler Bug
From: Rob Kennedy (me3_at_privacy.net)
Date: Fri, 24 Sep 2004 16:56:00 -0500
> VBDis wrote:
>> Sample code:
>> TBase = Object
>> s: string;
>> TDerived = Object(TBase)
>> procedure test;
>> var obj: TDerived; //okay for TBase!
It's OK for TBase, but only due to chance. Insert a ShowMessage call to
display s's value, and the assertion should fail, even if ShowMessage
comes *after* Assert.
>> assert(obj.s = '', 'uninitialized dynamic variable');
> the compiler is doing exactly what it should be doing, the string is
> treated like any standard string that you may create
> at the entry point of a block.
No, it's not. The Assert function is failing, showing that the string is
not being treated correctly.
> also take not that you are not using the dynamic type of objects which
> would be the Tobject in this case, but the objects which came first.
Yes, I think Dodi realizes that already, given his message started out
"The bug is related to the (deprecated) type Object ... ."
> i know its nice the delphi still supports these types and i am glad
> that they do.
Delphi does not fully support old-style objects anymore. They work
sometimes, but not always. Borland knows this.
> you need to initiate the code your self.
That looks like the only way.
FillChar(obj, SizeOf(obj), 0);
And then remember to clear the object's fields before the function
returns, since I don't think that's happening properly, either.
> its no different from the need to clear the string or define it to
> something on the entry of a code block.
Strings don't need to be cleared. The compiler does it automatically,
mainly because it has to for the rest of the string code in the
procedure to operate properly. Declare a local string variable, or a
local record variable that contains a string, and that variable will get
initialized automatically upon entry to the procedure. Declare a local
old-style object, though, and apparently, its string fields won't always
> the years of TP i did i never saw any docs stating that these objects
> were initiated automatically, you may also want to take note that this
> kind of string your using is not the default 255 byte long string but a
> dynamic string.
Turbo Pascal documentation doesn't mention anything like that because
Turbo Pascal had no automatically allocated heap data types like
AnsiString that would need anything like that.
> specify a size of 255 or less in the string instead and then you will
> have the exact of TP.
Change the declaration of the s field to ShortString, and you'll still
get the same error.
I added a field to TDerived, and the compiler appears to initialize it
since the assertion line succeeds, but if I also use the field in normal
code, it's apparent that TDerived field isn't initialized, either.