Jump to content
Sign in to follow this  
Die Holländer

How to free object compiled to Linux

Recommended Posts

I just started in Delphi 10.4 to compile to Linux and need to know how to handle Free of objects, like how to

handle the following code that compiles OK against Windows but not for Linux using the Delphi native JSon parser:

 

LStringReader := TStringReader.Create(aLoginJSON);
LJsonTextReader := TJsonTextReader.Create(LStringReader);

try

//parse the JSon string

finally

LStringReader.Free; <-- OK for both platforms
LJsonTextReader.Free; <-- Segmentation Fault under Linux

end;

 

How to handle LJsonTextReader.Free; without causing mem leaks?

Share this post


Link to post

I cannot verify the issue at the moment, but since TJsonTextReader uses TStringReader it might be better to reverse the releasing order. It is likely that TJsonTextReader destructor is accessing the TStringReader instance during destruction process. Such code would also be wrong on Windows, but you are just lucky that it does not fail there.

 

LJsonTextReader.Free; 
LStringReader.Free;

 

Share this post


Link to post
1 hour ago, Die Holländer said:

Thanks Dalija ! Yes, It seems i'm just lucky and wonder why its working OK under Windows but indeed it solved the issue.

On Windows Delphi uses FASTMM memory manager and on Other platforms it uses POSIX memory manager. I am not that familiar with inner workings of a POSIX memory manager, but FASTMM allocates larger chunks of memory from the OS which is then used for sub-allocating object instances and other heap based data. When you release an object, data in its memory location can be still intact and accessing such data does not always crash. In such cases memory is held by FASTMM so there will be no crash in the OS side because for the OS it is valid memory allocated by the program.

 

If you use FASTMM in full debug mode during the development, then accessing such invalid object will be noticed by FASTMM and shown as error.

Share this post


Link to post

TJsonTextReader calls Close during Destroy on its FReader and that as Dalija explained might still work on Windows because even though the object and its memory is not valid anymore it has not been reused yet.

 

You can test this quite easily with this code:

 

uses
  FastMM5,
  System.Classes,
  System.JSON.Readers;

begin
  FastMM_EnterDebugMode;
  var LStringReader := TStringReader.Create('');
  var LJsonTextReader := TJsonTextReader.Create(LStringReader);

  LStringReader.Free;
  LJsonTextReader.Free;
end.

And you get a nice "A virtual method was called on a freed object" exception resulting from the FReader.Close call in TJsonTextReader.Close

  • Like 1

Share this post


Link to post
19 minutes ago, Stefan Glienke said:

FastMM_EnterDebugMode;

Are you using fastmm5? I don't have this procedure in Fastmm4.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×