View Issue Details

IDProjectCategoryView StatusLast Update
0031197FPCRTLpublic2017-01-06 22:56
ReporterBart BroersmaAssigned ToJonas Maebe 
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Platformi386OSWindowsOS VersionWin7
Product VersionProduct Build 
Target VersionFixed 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;
begin
  writeln('SysErrorMessage(193)=',SysErrorMessage(193));
end.

Build with -gh

Free Pascal Compiler version 3.0.2rc1 [2016/12/20] for i386
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
Marked memory at $001C4D78 invalid
Wrong size : 255 allocated 510 freed
  $0040D639
  $00408C76
  $004015A8
Call trace for block $001C4D78 size 255
  $004015A8
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
  $004015A8
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
FPCOldBugId
FPCTarget
Attached Files

Relationships

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

Activities

Bart Broersma

2017-01-06 12:15

reporter   ~0097331

Last edited: 2017-01-06 12:18

View 2 revisions

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

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

Bart Broersma

2017-01-06 12:38

reporter   ~0097332

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

Bart Broersma

2017-01-06 13: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 22:56

reporter   ~0097344

Thanks.
Now waiting for RC2...

Issue History

Date Modified Username Field Change
2017-01-06 12:15 Bart Broersma New Issue
2017-01-06 12:15 Bart Broersma Note Added: 0097331
2017-01-06 12:18 Bart Broersma Note Edited: 0097331 View Revisions
2017-01-06 12:38 Bart Broersma Note Added: 0097332
2017-01-06 13:00 Bart Broersma Note Added: 0097335
2017-01-06 13: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 21:08 Jonas Maebe Relationship added related to 0029913
2017-01-06 21:19 Jonas Maebe Fixed in Revision => 35247
2017-01-06 21:19 Jonas Maebe Status new => resolved
2017-01-06 21:19 Jonas Maebe Fixed in Version => 3.0.2
2017-01-06 21:19 Jonas Maebe Resolution open => fixed
2017-01-06 21:19 Jonas Maebe Assigned To => Jonas Maebe
2017-01-06 22:56 Bart Broersma Note Added: 0097344
2017-01-06 22:56 Bart Broersma Status resolved => closed