<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title></title>
</head>
<body>
<p style="margin: 0px;"><span> </span></p>
<p style="margin: 0px;"> </p>
<div style="margin: 5px 0px 5px 0px;">
Martin <fpc@mfriebe.de> hat am 7. Juli 2011 um 12:20 geschrieben:<br/>
<br/>
> On 07/07/2011 07:51, Alexander Klenin wrote:<br/>
>[...]<br/>
> >> As I have shown before in this thread, non ref. counted values e.g.<br/>
> >> shortstrings are affected by the same problem.<br/>
> > And as per Chad's reply (and agree with his logic), this is not so.<br/>
><br/>
><br/>
> Well it's not as obvious)<br/>
><br/>
> In Florian's example the "const" param follows the value of the<br/>
> original variable (so yes it does something that is unexpected)<br/>
> But with short string it does not crash. there is no ref count going<br/>
> wrong, there is no memory being freed while still in use.<br/>
><br/>
> Yet he is 100% right it's the same problem, the same error, and as severe.<br/>
><br/>
> Because the behaviour is not guaranteed.<br/>
> The compiler (in future versions) could make optimizations knowing that<br/>
> the "const s: shortstring" (or "const x: sometype") doesn't change.
</div>
<p style="margin: 0px;"> </p>
<p style="margin: 0px;">You don't need the future. You can already create a crash using const shortstring:</p>
<p style="margin: 0px;"> </p>
<p style="margin: 0px;"> </p>
<p style="margin: 0px;">{$mode objfpc}{$H+}<br/>
uses Classes;<br/>
var<br/>
s: shortstring = '';<br/>
o: TStringList = nil;<br/>
<br/>
procedure DoSomething(const h: shortstring);<br/>
begin<br/>
if h<>'' then<br/>
o:=TStringList.Create; // not called, because s=h=''<br/>
s:='A';<br/>
if h<>'' then<br/>
writeln(o.Count); // called, because now s=h='A' => crash<br/>
end;<br/>
<br/>
begin<br/>
DoSomething(s);<br/>
end.</p>
<p style="margin: 0px;"> </p>
<p style="margin: 0px;"> </p>
<div style="margin: 5px 0px 5px 0px;">
>[...]
</div><br/>
<br/>
Mattias<br/>
<br/>
</body>
</html>