#1 2021-09-29 21:21:24

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Access Violation when using mORMot 2 / DLL / Runtime packages

Delphi 10.4

An application raises an exception on close.

---------------------------
Debugger Exception Notification
---------------------------
Project Project3.exe raised exception class EAccessViolation with message 'Access violation at address 5005FC74 in module 'rtl270.bpl'. Read of address 03592104'.
---------------------------
Break   Continue   Help   
---------------------------

Library:

library Project4;

uses
  mormot.core.variants;

{$R *.res}

begin
end.

Application

uses 
  mormot.core.variants;

procedure TForm.OnButtonClick(Sender: TObject);
begin
  var h := LoadLibrary('Project4.dll');
  FreeLibrary(h);
end;

Both application and dll compiled with checked Link with runtime packages with one package vcl


When I commented:

initialization
 // InitializeUnit;

in mormot.core.variants application closes without any errors.

Last edited by radexpol (2021-09-30 07:33:45)

Offline

#2 2021-09-30 04:17:06

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 239
Website

Re: Access Violation when using mORMot 2 / DLL / Runtime packages

Even though that *seems* to solve your problem, disable portions of your code and watch when that error disappears. As many of us are using that unit as well any many more projects without that problem, I would believe that some part of your code is producing that hiccup.

Offline

#3 2021-09-30 07:28:43

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: Access Violation when using mORMot 2 / DLL / Runtime packages

@sakura, I have no more code, nothing to disable. That problem appears when using runtime packages.

Offline

#4 2021-09-30 07:49:06

sakura
Member
From: Germany
Registered: 2018-02-21
Posts: 239
Website

Re: Access Violation when using mORMot 2 / DLL / Runtime packages

radexpol wrote:

@sakura, I have no more code, nothing to disable. That problem appears when using runtime packages.

Sorry, I missed that part with the runtime packages. Haven't tried that with mORMot 2, but design-time packages for the Delphi IDE never worked with mORMot 1 either. Slightly different, I know, but I believe that will be the case with run-time packages always, as mORMot is going deeply into the VMT and RTTI of the application. But let's see what Arnaud can tell :-)

Offline

#5 2024-03-07 16:29:49

TommyLee
Member
Registered: 2024-03-07
Posts: 1

Re: Access Violation when using mORMot 2 / DLL / Runtime packages

If anyone else has this problem: the error occurs in freeing memory for the dynamic array: SynVariantTypes. This array is created in mormot.core.variants initialization. It contains interfaced objects.
By default, the array and its objects are released when the application is closed via RTL finalization.
However, when using it in dll, an AV exception is generated.

The hack solution is to explicitly free the array before reaching the RTL.Finalization stage.

procedure FinalizeSynVariantTypes;
begin
  for var i: integer := 0 to length(SynVariantTypes) - 1 do
  TObject(SynVariantTypes[i]).Free;
  SetLength(SynVariantTypes, 0);
end;

For example, this procedure can be placed in the finalization part of another unit that uses mormot.core.variants (or mormot.core.log, which also includes this module)

Offline

Board footer

Powered by FluxBB