Re: D7 Compiler Bug

From: Rob Kennedy (me3_at_privacy.net)
Date: 09/24/04


Date: Fri, 24 Sep 2004 16:56:00 -0500

Jamie wrote:
> VBDis wrote:
>> Sample code:
>>
>> type
>> TBase = Object
>> public
>> s: string;
>> end;
>>
>> TDerived = Object(TBase)
>> end;
>>
>> 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.

>> begin
>> assert(obj.s = '', 'uninitialized dynamic variable');
>> end;
>>
>> DoDi
>
> 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
be initialized.

> 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.

-- 
Rob


Relevant Pages

  • Re: Record type flushing
    ... If you try to assign a new value to a string field in that ... reference, so the compiler's code tries to release that reference before ... The Initialize procedure can be used to solve that problem. ... If you're using the shell's memory manager, ...
    (alt.comp.lang.borland-delphi)
  • Re: CORE - Literal Instantiation breaks Object Model
    ... order to view the model (and the language in general) strictly from a ... before you redefine the initialize method. ... Ruby at compile time. ... String, those methods apply to String literals. ...
    (comp.lang.ruby)
  • Re: Lets make it backwards compatible :D
    ... > Incorrect use of Initialize. ... it is a string. ... The pointer storage would point to the null terminator. ... Storage now holds a string reference. ...
    (alt.comp.lang.borland-delphi)
  • Re: recursive function problem
    ... def show_params ... This allows you to keep internal knowledge inside, ... You still have an issue in that you only initialize the variable params_str ... and you cannot then concatenate additional string to it. ...
    (comp.lang.ruby)
  • Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
    ... string to string variables" lazy because it prevents the developer from ... It's to always initialize it to something when I declare it. ... Anyhow, please describe "potential problems?" ...
    (microsoft.public.dotnet.languages.vb)