[fpc-pascal] Untyped var params
Sven Barth
pascaldragon at googlemail.com
Sun Jan 8 11:22:47 CET 2017
On 08.01.2017 10:35, Ryan Joseph wrote:
>
>> On Jan 8, 2017, at 2:37 AM, Andrew Hall <andrew.hall at shaw.ca> wrote:
>>
>> If you cast your “something” to a typed pointer, the compiler will do the work for you in the usual way:
>>
>> PDouble(something)^ := myDouble;
>> PAnsiString(something)^ := myAnsiString;
>> myRecordStructure := PMyRecordStructure(something)^;
>
> I’m not getting any of this to work.
>
> Here is the pattern I was attempting. It’s a “release existing, retain and assign new” function which handles nil inputs safely. I thought I could use untyped params so I don’t need to typecast “io” to TObject when I call RetainObject every time.
>
> procedure RetainObject (var io; newObject: TObject);
> var
> obj: TObject absolute io;
> begin
> if obj <> nil then
> obj.Release;
> if newObject <> nil then
> begin
> obj := newObject;
> obj.Retain;
> end
> else
> obj := nil;
>
> TObjectPtr(io)^ := obj; // crashes here
> end;
1. Why are you trying to assign something to io when you already did
that with "obj := ..."?
2. TObject is an implicit pointer, so "TObject(io) := ..." would be
correct (see the implementation of FreeAndNil() in
$fpc/rtl/objpas/sysutils/sysutils.inc) [otherwise your usage of "obj"
would already crash]
Regards,
Sven
More information about the fpc-pascal
mailing list