<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;
      charset=iso-8859-2">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Thanks to Giuliano mentioning debug libraries I have been able to
      duplicate the problem and to find the source of the bug - but it
      is as weird as it gets.</p>
    <p>FYI: tests done on Linux Mint 18 with Lazarus 1.8.0 and fpc
      3.0.4.</p>
    <p>The evidence so far:</p>
    <p>1. Gabor's program ends with an exception when using the FPC
      release  RTL and FCL, but not when using RTL and FCL libraries
      compiled for debugging.</p>
    <p>2. If I use GDB to step through the final steps of the program,
      the bug disappears!</p>
    <p>3. If I add a delay at the end (e.g. a call to sleep(1)), the bug
      disappears.</p>
    <p>4. Playing around with code optimisations and debugging flags
      seems to have no effect.</p>
    <p>5. Using the age old technique of commenting out bits of the code
      until the bug disappears, I tracked the problem down.</p>
    <p>In Gabor's program, the bug is removed by commenting out this
      line (shown in context)</p>
    <p>      for i:=0 to SQR.GetCount-1 do<br>
             begin<br>
               case SQR[i].getItemType of isc_info_svc_line:begin<br>
      //                                                       
      s:=SQR[i].getAsString;  {This is where the problem starts}<br>
                                                            end;<br>
               end;<br>
               if (s<>'') then WriteLn(s);<br>
             end;<br>
      <br>
    </p>
    Going deeper into the fbintf package, I have further tracked the
    problem down to this line of code<br>
    <br>
    SetString(Result,FBufPtr+3,len,CP_ACP);<br>
    <br>
    FBufPtr is a pointer and it is fairly simple bit of code, copying a
    string from a buffer (received from Firebird) into an AnsiString.
    Commenting out the line stops the bug. The bug also goes away by
    changing it to<br>
    <br>
    var s: string;<br>
          i: integer;<br>
    <br>
    ...<br>
     Result := '';<br>
      SetString(s,FBufPtr+3,len,CP_ACP);<br>
      for i := 1 to length(s) do<br>
        Result += s[i];<br>
    <br>
    which can only be described as a WTF moment. The problem has to be
    due to string disposal.<br>
    <br>
    Finally, knowing all this, I go back to Gabor's program and add the
    line (again shown in context):<br>
    <br>
       repeat<br>
          SQR:=SM.Query(nil,SRB);<br>
          for i:=0 to SQR.GetCount-1 do<br>
           begin<br>
             case SQR[i].getItemType of isc_info_svc_line:begin<br>
                                                           
    s:=SQR[i].getAsString;<br>
                                                          end;<br>
             end;<br>
             if (s<>'') then WriteLn(s);<br>
             s := '';                                             {Line
    added here}<br>
           end;<br>
        until (s='');<br>
    <br>
    and guess what - bug goes away.<br>
    <br>
    It looks like what is happening is that SetString is setting an
    AnsiString in such as away as to cause a problem when the memory
    manager cleans up - but only as a race condition and if the string
    is not cleaned up explicitly.<br>
    <br>
    Looks like an FPC bug report to me.<br>
    <br>
    <div class="moz-cite-prefix">On 09/03/18 09:14, Gabor Boros via
      Lazarus wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:3cef622d-8155-25d7-65b2-647d649d6374@bgss.hu">Hi All, <br>
      <br>
      The result of the attached example (which use MWA's Firebird
      Pascal API) for me is an exception: <br>
      <br>
      <br>
      Gstat execution time Fri Mar  9 09:29:18 2018 <br>
      <br>
      Database header page information: <br>
              Flags                   0 <br>
              Generation              173 <br>
              System Change Number    0 <br>
              Page size               8192 <br>
              ODS version             12.0 <br>
              Oldest transaction      161 <br>
              Oldest active           162 <br>
              Oldest snapshot         162 <br>
              Next transaction        164 <br>
              Sequence number         0 <br>
              Next attachment ID      27 <br>
              Implementation          HW=AMD/Intel/x64 little-endian
      OS=Linux CC=gcc <br>
              Shadow count            0 <br>
              Page buffers            0 <br>
              Next header page        0 <br>
              Database dialect        3 <br>
              Creation date           Feb 2, 2018 14:07:24 <br>
              Attributes              force write <br>
      <br>
          Variable header data: <br>
              *END* <br>
      Gstat completion time Fri Mar  9 09:29:18 2018 <br>
      <br>
      Heap dump by heaptrc unit <br>
      355 memory blocks allocated : 948252/948432 <br>
      355 memory blocks freed     : 948252/948432 <br>
      0 unfreed memory blocks : 0 <br>
      True heap size : 131072 <br>
      True free heap : 131072 <br>
      An unhandled exception occurred at $00007FF97F0A3147: <br>
      EAccessViolation: <br>
        $00007FF97F0A3147 <br>
      <br>
      <br>
      If comment out cthreads from the uses the exception is: <br>
      <br>
      <br>
      An unhandled exception occurred at $00007F21B71F5147: <br>
      EAccessViolation: <br>
        $00007F21B71F5147 <br>
        $00007F21B71F751B <br>
        $00007F21B71F769C <br>
        $00007F21B746BFFF <br>
      <br>
      <br>
      If run with gdb (and cthreads): <br>
      <br>
      <br>
      Heap dump by heaptrc unit <br>
      355 memory blocks allocated : 948252/948432 <br>
      355 memory blocks freed     : 948252/948432 <br>
      0 unfreed memory blocks : 0 <br>
      True heap size : 131072 <br>
      True free heap : 131072 <br>
      [Thread 0x7ffff5991700 (LWP 2874) exited] <br>
      [Thread 0x7ffff6192700 (LWP 2873) exited] <br>
      [Thread 0x7ffff7fe3740 (LWP 2869) exited] <br>
      Cannot find user-level thread for LWP 2875: generic error <br>
      <br>
      <br>
      Any idea how to detect what/where is the source of the exception?
      <br>
      <br>
      I use Linux 64bit, FPC 3.0.4 and Lazarus fixes_1_8. <br>
      <br>
      Gabor <br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
    </blockquote>
    <br>
  </body>
</html>