[Lazarus] strcat problem

Yann Mérignac yann.merignac at gmail.com
Fri Jun 24 10:22:12 CEST 2016


1 cTekst:=StrAlloc(600);
2 cTekst:=#13'Now is the winter of our discontent'#13;
3 cTekst:=StrCat(cTekst,'Made glorious summer by this sun of York;'#13);

On line 2 you change the value of cTekst and lose the memory allocated by
StrAlloc. What you want to do is to copy the string constant value in the
allocated memory with StrCopy or StrMove. See
http://www.freepascal.org/docs-html/rtl/strings/strcat.html

Another solution:

var
  S : String;
  cTekst : PChar;
begin
   S := #13'Now is the winter of our discontent'#13;
   S := S + 'Made glorious summer by this sun of York;'#13;
   cTekst := @S[1];
end;


2016-06-24 9:43 GMT+02:00 Koenraad Lelong <lazarus2 at de-brouwerij.be>:

> Op 23-06-16 om 21:38 schreef Martin Grajcar:
>
>> Have you checked if there's any room behind #13'Now is the winter of our
>> discontent'#13, so that someone else's memory won't get overwritten? I'd
>> bet, there's none.
>>
>> /Appends a copy of the source string to the destination string. The
>> terminating null character in destination is overwritten by the first
>> character of source, and a null-character is included at the end of the
>> new string formed by the concatenation of both *in destination*./
>>
>> You'd need something like
>> SetLength(cTekst, enoughToHoldThemAll);
>> and maybe also
>> cTekst[oldLength] := #0;
>> before the very first strcat.
>>
>> Updated my code :
>
>  cTekst:=StrAlloc(600);
>  cTekst:=#13'Now is the winter of our discontent'#13;
>  cTekst:=StrCat(cTekst,'Made glorious summer by this sun of York;'#13);
>  ...
>
> cTekst is still a PChar.
>
> Same problem : on the pi the program crashes on the first StrCat with a
> 216 error.
>
> An extra error, now on my OpenSuse-box :
> I finally do
> StrDispose(cTekst);
> And then the application crashes with a 216 error. Leaving that StrDispose
> out it runs fine, but then there's a memory-leak.
>
> FWIW, I'm using FPC 2.6.5 (SVN version 49333M) on OpenSuse and 3.0.0 on
> the pi.
>
>
> Koenraad
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus-ide.org
> http://lists.lazarus-ide.org/listinfo/lazarus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160624/c5a52f3b/attachment-0002.html>


More information about the Lazarus mailing list