#1 Low level and performance » Merging FastMM4 and tsynlog data for better memory leak debugging » 2025-04-01 14:09:58

docme
Replies: 0

I've had too many weird memory leaks in my programs, so I had to dig around and put this together.  Thought it might help someone.  I was wondering if there is a better way of finding those bugs that think they are right to be there.  My idea was to collect the memory allocation number used by FastMM4 and save it in the .log file generated by tsynlog, so I can find 'when' as well as 'where' the memory was being allocated that wasn't being released.

This is in the main .dpr, uses clause and around the main begin section. 

uses
  FastMM4, syncommons, ...

...

{$Include FastMM4Options.inc} // must set FullDebugModeCallBacks in the used FastMM4Options.inc file!

{$ifdef debug}
{$ifdef FullDebugModeCallBacks}
procedure MyOnDebugGetMemFinish(APHeaderNewBlock: PFullDebugBlockHeader; ASize: NativeInt);
begin
// We can't use ILog, as it allocates memory = infinite loop!
//var ILog: ISynLog; begin  ILog := TSynLogDebug.Enter(self); //,pointer(rawutf8( inttostr(APHeaderNewBlock.AllocationNumber) + ' ' + inttostr(Asize) )));
  syncommons.FastMemAllocNumber := APHeaderNewBlock.AllocationNumber;
end;
{$endif}
{$endif}

begin
  {$ifdef debug}
  {$ifdef FullDebugModeCallBacks}
  OnDebugGetMemFinish := MyOnDebugGetMemFinish;
  {$endif}
  ReportMemoryLeaksOnShutdown := True; // seems lots!
  {$endif}

...

This piece of code I added to syncommons.pas just above the implementation reserved word.  No idea why I didn't put it into synlog.pas.

/// Code in .dpr sets event OnDebugGetMemFinish := MyOnDebugGetMemFinish; 
/// procedure MyOnDebugGetMemFinish(APHeaderNewBlock: PFullDebugBlockHeader; ASize: NativeInt); sets  syncommons.FastMemAllocNumber := APHeaderNewBlock.AllocationNumber;
/// Code in synLog.pas writes out current syncommons.FastMemAllocNumber before the line number in brackets near the end of the enter line.
var
  FastMemAllocNumber : Cardinal;

And the last piece to make it all actually do something in synlog.pas somewhere near the end of class procedure TSynMapFile.Log between a ' ' and a '('.  In my mormot1 version dated 20240808, it's line 2293.  SynLog.pas changed so often, I'm not sure if your one looks remotely the same as mine.

    if FastMemAllocNumber <> 0 then
      W.Add(FastMemAllocNumber);

Only thing left to do now is replace all begins with

var ILog: ISynLog; begin  ILog := TSynLogDebug.Enter(self);

and when the memory leak shows the allocation numbers, we can trace to them more accurately in our log file(s).

#2 Re: mORMot 1 » Delphi2007 [Pascal Fatal Error] SynLog.pas(2745): F2084 Internal Error » 2015-06-18 07:18:28

Found it, installed it and seems the problem has gone.  Thanks.
I see there are a bunch of IDE fixes there on Andy's website.
So I installed the others too.  My c:\installs\fixesforDelphi directory is getting full.
Not sure now about the vclfixpack.pas whether it conflicts with syncommons.pas and whether it should go before or after it in uses clause.

#3 Re: mORMot 1 » Delphi2007 [Pascal Fatal Error] SynLog.pas(2745): F2084 Internal Error » 2015-06-17 18:12:37

Yes, that's true.  Sorry, I didn't state in my original post that it compiles fine.  It's just the IDE gives that error and as a result some of the IDE assist features do not work so I had to figure out some sort of workaround or I'm stuck with a lot of asynchronous event driven code to debug and no help from the IDE.  I will most likely forget to uncomment the line when compiling for test or live.  I'm guessing if I moved that {$STACKFRAMES ON} section to the end of the unit just before the initialization section, it would be less of an issue.

#4 mORMot 1 » Delphi2007 [Pascal Fatal Error] SynLog.pas(2745): F2084 Internal Error » 2015-06-17 14:34:53

docme
Replies: 4

In Delphi 2007 (Version 11.0.2902.10471) IDE, I'm getting an IDE error when using relatively new (1 day old download of) synlog.pas from mORMot_and_Open_Source_friends_2015-06-16_112651_4986fea04f.
If I comment out the {$STACKFRAMES OFF} with '//', then it doesn't keep repeating '[Pascal Fatal Error] SynLog.pas(2745): F2084 Internal Error: AV06419F0F-W0000001C-1' in the build messages view.
Not sure if this causes issues I don't know about.

#5 Low level and performance » CSV faster code » 2014-03-28 08:53:16

docme
Replies: 1

Existing AddCSV code:

  for i := 0 to high(Integers) do begin
    Add(Integers[i]);
    Add(',');
  end;
  CancelLastComma;

faster code?:

  Add(Integers[0]);
  for i := 1 to high(Integers) do begin
    Add(',');
    Add(Integers[i]);
  end;

// for works same as a while loop.

Board footer

Powered by FluxBB