<div dir="ltr"><div>Michael, thanks for your feedback. <br><br>One thing that confuses
me in light of Jonas' reply, if what you say is correct (that local
variables that have just come into scope are not guaranteed to be nil)
then assignment of Result := ''; at the first line of foo may
arbitrarily SEGV because fpc_AnsiStr_Decr_Ref will interpret the
(possibly) non-nil value (of Result) as an AnsiString which (being a
random uninitialized value) will likely be incorrect and blow up.<br>
<br>
Surely the semantics of string handling relies on FPC guaranteeing
automatic variables are always preassigned nil when they come into
scope?<br>
<br>
Put another way, how does fpc_AnsiStr_Decr_Ref and friends,
which receive the address of the caller's Result variable via their var
parameter know that the value of this parameter (which may not be
initialized if what you say is correct) is or is not a valid string?<br><br></div>Bruce.<br><div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 8, 2013 at 5:18 PM, Michael Van Canneyt <span dir="ltr"><<a href="mailto:michael@freepascal.org" target="_blank">michael@freepascal.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
<br>
On Wed, 8 May 2013, Bruce Tulloch wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
After a random but very long period of time (i.e. very many successful calls) I get a SEGV in the built-in function fpc_AnsiStr_Decr_Ref.<br>
<br>
GDB reports the argument to fpc_AnsiStr_Decr_Ref (the string who's reference is to be decremented) is nil (i.e. 0x0).<br>
<br>
Prima facie, that's the reason for the SEGV, but how is it possible that the compiler would pass a nil pointer to this function the first place?<br>
<br>
To put this into context, I'm running FPC 2.6.2 on a 32 bit Linux system executing in a multi-threaded application (which uses python threads and fpc threads). I have not found obvious<br>
evidence of memory corruption from other execution contexts or shared memory handling problems.<br>
<br>
The SEGV occurs when called from a function, let's call it foo, that looks like this:<br>
<br>
function foo : AnsiString;<br>
begin<br>
Result := '';<br>
<other stuff><br>
end;<br>
<br>
The AnsiString pointer that fpc_AnsiStr_Decr_Ref throws a SEGV is Result, at the first line of the function foo.<br>
<br>
It appears the compiler is passing Result to fpc_AnsiStr_Decr_Ref even though Result (at this point in the function) must be nil (having only just come into scope).<br>
</blockquote>
<br></div></div>
This is not correct. Result is NOT guaranteed to be nil.<br>
<br>
About a year ago, I was as surprised as you are to discover this, but it is so.<br>
It is even so in Delphi.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
How is is possible that fpc_AnsiStr_Decr_Ref is being called at all?<br>
</blockquote>
<br></div>
Roughly:<br>
<br>
What happens is that the caller gives the address of the location where the result must go.<br>
The function receives this address, and then treats it as a normal variable, meaning that as soon as it is used, fpc_AnsiStr_Decr_Ref and friends come into play.<br>
<br>
The exact behaviour also depends on the compiler version.<br>
<br>
One of the compiler maintainers can describe this in more detail.<span class="HOEnZb"><font color="#888888"><br>
<br>
Michael.</font></span><br>_______________________________________________<br>
fpc-pascal maillist - <a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/mailman/listinfo/fpc-pascal" target="_blank">http://lists.freepascal.org/mailman/listinfo/fpc-pascal</a><br></blockquote></div><br></div></div></div>