Home Strange StrPCopy() AV error and workaround that does not seems to make sense
Reply: 0

Strange StrPCopy() AV error and workaround that does not seems to make sense

user911
1#
user911 Published in April 24, 2018, 10:30 am

I am running into a strange problem dealing with StrPCopy(). Please take a look at the sample codes below:

procedure TForm2.butnTestClick(Sender: TObject);
var
  s : string;
begin
  //-- assign string this way will cause AV when trying to StrPCopy()
  s := 'original string';

  //-- assign string this way works!!!!!!!
  //s := Trim('original string');

  //-- AV error when trying to alter the string
  StrPCopy(PChar(s), PChar('changed'));

  //-- should come back with "changed"
  Memo1.Lines.Add(s);
end;

I am using Delphi 10 Seattle. If I try to alter "s" using StrPCopy() I will get AV error. However, I surround my string declaration with Trim(), it will work.

Seems like by surrounding string declaration with Trim() triggers compiler to turn off some sort optimization on that particular string. I just don't know what that is. Please help.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.32819 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO