View Issue Details

IDProjectCategoryView StatusLast Update
0036212FPCCompilerpublic2019-11-03 20:22
ReporterOndrej PokornyAssigned ToFlorian 
Status closedResolutionfixed 
Platformwin 32bitOSWindows 10OS Version
Product Version3.3.1Product Buildr43309 
Target VersionFixed in Version3.3.1 
Summary0036212: -O3 memory corruption with record assignments to function result
DescriptionCompile the program in "Steps To Reproduce".

With -O2 it works as expected.
Winth -O3 the MyObj variable gets corrupted in the Test method call (the second Writeln writes a different pointer value).
Steps To Reproduceprogram Project1;
{$mode delphi}
  TSinglePoint = record
    X, Y: Single;
    constructor Create(const aX, aY: Single);
{ TSinglePoint }
constructor TSinglePoint.Create(const aX, aY: Single);
  X := aX;
  Y := aY;

  TMyObj = class
    function Test: TSinglePoint;
{ TMyObj }
function TMyObj.Test: TSinglePoint;
  Result := TSinglePoint.Create(0, 0);

  MyObj: TMyObj;
  MyObj := TMyObj.Create;
TagsNo tags attached.
Fixed in Revision
Attached Files


Thaddy de Koning

2019-10-25 07:17

reporter   ~0118799

The code works correct on 32 bit ARM. looks i386 specific

Serge Anvarov

2019-10-25 21:17

reporter   ~0118808

x64, Windows 7, FPC 3.3.1 x64. No problems other than memory leak.
FPC 3.0.4 x32. No problems other than memory leak.

Do-wan Kim

2019-10-27 02:44

reporter   ~0118865

Last edited: 2019-10-27 06:30

View 2 revisions

It corrupt stack after 'MyObj.Test' call. It make invalid, caused by %ebp value to %ebx.

temporary fix, add dummy member in record.

  TSinglePoint = record
    X, Y: single;
    dummy: single;
    constructor Create(const aX, aY: single);


2019-11-03 11:36

administrator   ~0119006

I cannot reproduce it with FPC i386-win32 from today. Can please post the full command line?

Ondrej Pokorny

2019-11-03 12:53

developer   ~0119008

I confirm that the bug is gone in current trunk (r43374). I can also confirm that the bug was present in r43294 (I didn't update FPC on one machine).

So yes, somebody fixed it in between.

Would it be worth to add a test case to the FPC compiler test suite? It was quite a dangerous bug and I needed quite some time to understand that it was a compiler bug and not a memory corruption in my program.


2019-11-03 18:34

administrator   ~0119017

I have added a test in r43385.

Ondrej Pokorny

2019-11-03 20:22

developer   ~0119019

Thank you!

Issue History

Date Modified Username Field Change
2019-10-25 00:02 Ondrej Pokorny New Issue
2019-10-25 07:17 Thaddy de Koning Note Added: 0118799
2019-10-25 21:17 Serge Anvarov Note Added: 0118808
2019-10-27 02:44 Do-wan Kim Note Added: 0118865
2019-10-27 06:30 Do-wan Kim Note Edited: 0118865 View Revisions
2019-11-03 11:36 Florian Note Added: 0119006
2019-11-03 12:53 Ondrej Pokorny Note Added: 0119008
2019-11-03 18:34 Florian Assigned To => Florian
2019-11-03 18:34 Florian Status new => resolved
2019-11-03 18:34 Florian Resolution open => fixed
2019-11-03 18:34 Florian Fixed in Version => 3.3.1
2019-11-03 18:34 Florian FPCTarget => -
2019-11-03 18:34 Florian Note Added: 0119017
2019-11-03 20:22 Ondrej Pokorny Status resolved => closed
2019-11-03 20:22 Ondrej Pokorny Note Added: 0119019