#1 2021-05-07 03:48:53

wxinix
Member
Registered: 2020-09-07
Posts: 121

Does mORMot work with FastMM5?

I am using mORMot 1.18 latest.

I found if I include FastMM5, while turn on the "full debug mode" - then my mORMot application throws all weird access violation errors.  However, if "full debug mode" is off, while only including FastMM5 in the project, there is not access violations.

Any one has encountered the same? Any advice?

Offline

#2 2021-05-07 06:18:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: Does mORMot work with FastMM5?

Which Delphi compiler are you using?

Check is you don't not have two memory managers enabled at the same time.
For instance, FastMM4 may be enabled or our SynFpcx64MM.pas unit may be enabled by SynDprUses.inc if it has been added to your .dpr file.

We checked mORMot against memory leaks and corruption with both FastMM4 in full debug mode and also with FPC HeapTrc option.
Some time ago, ValGrid was used for even more deeper testing on some production code.
I made some minimal tests with FastMM5, but since it is not allowed for commercial apps, and not compatible with FPC, we don't use it and we wrote our FastMM4 fork as SynFpcx64MM.

Check if the Access Violation comes from mORMot code, or not from your code.
The regression tests (TestSQL3.dpr) should run with no memory leak.

A minimal reproducible example may help.

Offline

#3 2021-05-07 13:01:06

wxinix
Member
Registered: 2020-09-07
Posts: 121

Re: Does mORMot work with FastMM5?

I use Delphi 10.4.2

I'd like to be consistent with mORMot.  So I'll forget about FastMM5, and use mORMot's version of FastMM4, but mORMot's FastMM4 doesn't seem to catch a very simple memory leak?  Does it require FullDebugMode.dll?  What is the correct way to use mORMot's FastMM4 (for better performance and memory safety)?


program Project1;

uses
  FastMM4 in 'FastMM4.pas', // mORMot's version of FastMM4
  Vcl.Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  ReportMemoryLeaksOnShutDown := True;
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

procedure TForm1.FormCreate(Sender: TObject);
begin
  var LLeakedObject := TObject.Create;
end;

Here is the small project:

Last edited by wxinix (2021-05-07 13:11:09)

Offline

#4 2021-05-07 14:52:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: Does mORMot work with FastMM5?

This is not specific to mORMot, so you would rather get more information about FastMM4 using Google and some online tutorial.

You can use mORMot's FastMM4 or any other version.
But with FastMM4 you need to set properly the FAstMM4 conditionals. ReportMemoryLeaksOnShutDown is for the Delphi internal MM IIRC.
And for FULLDEBUGMODE the dll is required.

Offline

#5 2021-05-07 15:24:31

wxinix
Member
Registered: 2020-09-07
Posts: 121

Re: Does mORMot work with FastMM5?

ab wrote:

Check if the Access Violation comes from mORMot code, or not from your code.
The regression tests (TestSQL3.dpr) should run with no memory leak.

A minimal reproducible example may help.

I checked TestSQL3.dpr with FastMM5, with FullDebugMode = ON,  and everything is fine (as always with mORMot!)

Then I found out all of the access-violation comes from this code:

function TSQLModel.SafeRoot: RawUTF8;
begin
  if self=nil then
    result := '' else
    result := fRoot;  // <----------------  access violation happens here, as reported by FastMM5, as "invalid Read of address 80808078".
end;

From FastMM5 manual, it says: "In debug mode freed memory blocks will be filled with the byte pattern  $808080... so that usage of a freed memory block or object, as well as corruption of the block header and/or footer will likely be detected."

So my guess is, fRoot in the above code is already freed, and filled with $80808078.  Then the line result := fRoot will trigger FastMM5 report access violation.

@ab - what do you think and advise for this situation?  I guess I'll just turn off "FullDebugMode"?

Offline

#6 2021-05-07 19:38:19

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: Does mORMot work with FastMM5?

If TestSQL3 is fine, then the problem is in your code.

You are freeing the TSQLModel too soon.

Offline

#7 2021-05-07 19:50:49

wxinix
Member
Registered: 2020-09-07
Posts: 121

Re: Does mORMot work with FastMM5?

ab wrote:

If TestSQL3 is fine, then the problem is in your code.
You are freeing the TSQLModel too soon.

Hello @ab  Many thanks for the confirmation.   You made my day.  Following your hint, I found the issue.  Thanks again!

This issue won't be detected by Delphi's default MM, and only surfaces up with FastMM in FullDebugMode. And the lesson I learned is: If TSQLRestServerDB holds a reference to TSQLModel, then TSQLModel must be free-ed AFTER TSQLRestServerDB.

destructor TMormotDatabase.Destroy;
begin
  FSQLModel.Free; // <-- this TSQLModel is being free-ed BEFORE TSQLRestServerDB.
  FSQLDBConnectionProps.Free;
  FServer.Free; // <-- FastMM5 in FullDebugMode (and ONLY FullDebugMode) will detect access violation to an already free-ed TSQLModel.

  inherited;
end;

Last edited by wxinix (2021-05-07 20:16:48)

Offline

#8 2021-05-08 06:14:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,548
Website

Re: Does mORMot work with FastMM5?

Congrats!

As an alternative, you could let the TSQLREstServerDB own the model.
Please see the samples.

Offline

Board footer

Powered by FluxBB