View Issue Details

IDProjectCategoryView StatusLast Update
0031197FPCRTLpublic2017-01-06 21:56
ReporterBart Broersma Assigned ToJonas Maebe  
Status closedResolutionfixed 
Fixed in Version3.0.2 
Summary0031197: SysErrorMessage crashes whith HeapTrace enabled (fpc 3.0.2rc1).
DescriptionA call to SysErrorMessage will crash a program if heaptrace is enabled.
This happens with fpc 3.0.2RC1 (which I cannot select from the "Product version" dropdown list).
Steps To Reproduceprogram test;

uses sysutils;

Build with -gh

Free Pascal Compiler version 3.0.2rc1 [2016/12/20] for i386
Marked memory at $001C4D78 invalid
Wrong size : 255 allocated 510 freed
Call trace for block $001C4D78 size 255
SysErrorMessage(193)=Heap dump by heaptrc unit
48 memory blocks allocated : 1385/1496
47 memory blocks freed : 1640/1752
1 unfreed memory blocks : -255
True heap size : 196608 (112 used in System startup)
True free heap : 196160
Should be : 196688
Call trace for block $001C4D78 size 255
Additional InformationIt crashes on win32 and win64.
I did not test any other environment.
(The value of the argument to SysErrorMessage does not matter)

The crash does not occur with 3.0.0 or trunk (r35221).

Please fix this before releasing 3.0.2, since it makes debugging my programs virtually impossible.
TagsNo tags attached.
Fixed in Revision35247
Attached Files


related to 0029913 resolvedMichael Van Canneyt Memory leak in sysutils.pp 


Bart Broersma

2017-01-06 11:15

reporter   ~0097331

Last edited: 2017-01-06 11:18

View 2 revisions

function SysErrorMessage(ErrorCode: Integer): String;
  MaxMsgSize = Format_Message_Max_Width_Mask;
  MsgBuffer: pChar;
  GetMem(MsgBuffer, MaxMsgSize); //<<-----------
  FillChar(MsgBuffer^, MaxMsgSize, #0);
                 MakeLangId(LANG_NEUTRAL, SUBLANG_DEFAULT),
                 MsgBuffer, { This function allocs the memory }
                 MaxMsgSize, { Maximum message size }
  SysErrorMessage := MsgBuffer;
  FreeMem(MsgBuffer, MaxMsgSize*2); //<<-----------

The call to FreeMem tries to free 2 times the amount of memory that was allocated using GetMem.

Bart Broersma

2017-01-06 11:38

reporter   ~0097332

The regression is caused by r33823 (merging of r33363 to fixes branch).

Bart Broersma

2017-01-06 12:00

reporter   ~0097335

The summary should read (fpc 3.0.2rc1), not (fpc 3.0.0rc1).
Can somebody update that please?

Bart Broersma

2017-01-06 21:56

reporter   ~0097344

Now waiting for RC2...

Issue History

Date Modified Username Field Change
2017-01-06 11:15 Bart Broersma New Issue
2017-01-06 11:15 Bart Broersma Note Added: 0097331
2017-01-06 11:18 Bart Broersma Note Edited: 0097331 View Revisions
2017-01-06 11:38 Bart Broersma Note Added: 0097332
2017-01-06 12:00 Bart Broersma Note Added: 0097335
2017-01-06 12:08 Luiz Americo Summary SysErrorMessage crashes whith HeapTrace enabled (fpc 3.0.0rc1). => SysErrorMessage crashes whith HeapTrace enabled (fpc 3.0.2rc1).
2017-01-06 20:08 Jonas Maebe Relationship added related to 0029913
2017-01-06 20:19 Jonas Maebe Fixed in Revision => 35247
2017-01-06 20:19 Jonas Maebe Status new => resolved
2017-01-06 20:19 Jonas Maebe Fixed in Version => 3.0.2
2017-01-06 20:19 Jonas Maebe Resolution open => fixed
2017-01-06 20:19 Jonas Maebe Assigned To => Jonas Maebe
2017-01-06 21:56 Bart Broersma Note Added: 0097344
2017-01-06 21:56 Bart Broersma Status resolved => closed