On 08.01.2017 10:35, Ryan Joseph wrote:
> 
>> On Jan 8, 2017, at 2:37 AM, Andrew Hall <andrew.h...@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
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to