Die Holländer 45 Posted January 19, 2023 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
Dalija Prasnikar 1396 Posted January 19, 2023 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
Die Holländer 45 Posted January 19, 2023 Thanks Dalija ! Yes, It seems i'm just lucky and wonder why its working OK under Windows but indeed it solved the issue. Share this post Link to post
Fr0sT.Brutal 900 Posted January 19, 2023 Maybe TJsonTextReader takes ownership on stringreader and frees it internally? Share this post Link to post
Dalija Prasnikar 1396 Posted January 19, 2023 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
Stefan Glienke 2002 Posted January 19, 2023 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 1 Share this post Link to post
Lajos Juhász 293 Posted January 19, 2023 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
Stefan Glienke 2002 Posted January 19, 2023 19 minutes ago, Lajos Juhász said: Are you using fastmm5? I assumed it would be obvious from the uses Share this post Link to post