[fpc-devel] Bug in PWidechar and refcounting

peter green plugwash at bircd.org
Sun Feb 13 02:36:58 CET 2005


this is not a bug

if when someone does pcharvar := ansistringvar or pwidecharvar :=
widestringvar we increased the refcount every program that used that
construct would leak memory.

where compiler automated types like ansistring and widestring meet non
automated types like pchar and pwidechar some care is required on the part
of the developer. If you don't like this move to a nanny state language like
java.

> -----Original Message-----
> From: fpc-devel-bounces at lists.freepascal.org
> [mailto:fpc-devel-bounces at lists.freepascal.org]On Behalf Of Joost van
> der Sluis
> Sent: 13 February 2005 00:32
> To: FPC developers' list
> Subject: [fpc-devel] Bug in PWidechar and refcounting
>
>
> Hi all,
>
> the bug-report on the website doesn't work, so I write a mail:
>
> If you assign a widechar to a pwidechar, the refcount isn't
> increased. This construct is often used in the variants-code, which is
> allowed, according to the comments at the top of wstrings.inc.
>
> An example:
>
> Program test;
>
> var PW    : PWideChar;
>     dummy : AnsiString;
>
> procedure Set_PW;
>
> var
>   W : WideString;
>   s : ansistring;
>
> begin
>   s := 'Hello world'; // Can't assign directly to W, because that's
>   W := s;             // interpreted as a constant with a refcount of -1
>
>   PW := PWideChar(W); // <- Bug, refcount of W (and PW) isn't increased
> end;                  // <- Memory at @W is deallocated, since refcount=0
>
> begin
>   Set_PW;
>   SetLength(Dummy,3);   // The dummy is allocated at the same
> address as PW
>   writeln(pchar(PW)[0]); // Should write 'H', but it doesn't
> end.
>
>
> JoJo,
>   Joost.
>
>
>
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel





More information about the fpc-devel mailing list