AnsiString problem in Android lazarus / 43-Android ---------------------------------------------------------------------- neuro · 2025-10-21 05:34 AnsiString<->String type castings are done automatically in Windows/Linux/macOS versions of Lazarus. However in Android version of Lazarus you need to be very careful not to forget to do all explicit castings of from AnsiString to String (and also from String to AnsiString). Below are examples which show the problem. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i integer_variable: Integer;string_variable: AnsiString;ansistring_variable: AnsiString;  string_variable := '12345';ansistring_variable := AnsiString('67890');  integer_variable := StrToInt(ansistring_variable);  // <<------- this line crashes Android app  integer_variable := StrToInt(String(ansistring_variable)); // <<------- this line works correctly as expected integer_variable := StrToInt(string_variable);  // <<------- this line works correctly as expected   ansistring_variable := string_variable; // <<------- this line will corrupt data of many random string variables and after some time later will crash Android appstring_variable := ansistring_variable; // <<------- this line will corrupt data of many random string variables and after some time later will crash Android app  ansistring_variable := AnsiString(string_variable); // <<------- this line works correctly as expectedstring_variable := String(ansistring_variable); // <<------- this line works correctly as expected In Windows/Linux/macOS versions of Lazarus you do not need to do all explicit castings of from AnsiString to String (and also from String to AnsiString). In Windows/Linux/macOS versions of Lazarus program the following code works without any crashing: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i integer_variable := StrToInt(ansistring_variable);ansistring_variable := string_variable;string_variable := ansistring_variable; Can anybody explain the reasons why Android version of Lazarus has problems with AnsiString? ---------------------------------------------------------------------- cdbc · 2025-10-21 05:40 Hi Have you by any chance, forgotten or deleted this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+}in your units and lpr-file?!? Regards Benny ---------------------------------------------------------------------- neuro · 2025-10-21 06:17 Quote from: cdbc on October 21, 2025, 07:40:48 amHave you by any chance, forgotten or deleted this:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+}in your units and lpr-file?!? Multiple units in my project contain the line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+} The documentation says this line controls the interpretation of Strings: https://wiki.freepascal.org/Mode_ObjFPC ---------------------------------------------------------------------- neuro · 2025-10-27 08:07 I have found another problem with Strings in Android platform. My program has a function which returns the result as PAnsiChar. The function has local String variable, which after concatenation of multiple strings is converted into PAnsiChar and is returned from function as the result. This program code works correctly in Windows/Linux/macOS, however in Android it fails to work – testing in Android showed that PAnsiChar result from function is very unstable – sometimes the returned result is correct, however sometimes it returns garbage and as a result the program might crash. I have found workaround solution to this problem. Now I use global String variable to store the result of a function, and function now returns PAnsiChar which is casted from global String variable, and now it works correctly in Android. Local String variables (inside functions) fail to work in Android. ---------------------------------------------------------------------- H₂SO₄ · 2025-10-27 08:51 Quote from: neuro on October 27, 2025, 09:07:16 amThe function has local String variable, which after concatenation of multiple strings is converted into PAnsiChar and is returned from function as the result. You're returning a dangling pointer to a freed  AnsiString  instance. This is undefined behavior - it's a pure coincidence that this happened to work on Windows. When dealing with raw pointers you'll have to manage memory yourself. Why are you returning a pointer, anyway - do you call that function from code written in another language? ---------------------------------------------------------------------- neuro · 2025-10-27 14:31 Quote from: Khrys on October 27, 2025, 09:51:02 amWhy are you returning a pointer, anyway - do you call that function from code written in another language? The program code of this function is compiled in two ways: 1) as a DLL, 2) as a EXE application. When program code is compiled as DLL, the result of exported function must be (C language) char* type, which is equivalent to Lazarus PAnsiChar type. The output result of DLL function in (C language) char* type can be used in all programming languages and compilers. ---------------------------------------------------------------------- PascalDragon · 2025-10-27 21:05 Quote from: neuro on October 27, 2025, 03:31:28 pmQuote from: Khrys on October 27, 2025, 09:51:02 amWhy are you returning a pointer, anyway - do you call that function from code written in another language? The program code of this function is compiled in two ways: 1) as a DLL, 2) as a EXE application. When program code is compiled as DLL, the result of exported function must be (C language) char* type, which is equivalent to Lazarus PAnsiChar type. The output result of DLL function in (C language) char* type can be used in all programming languages and compilers. But this nevertheless means that the returned value needs to survive the function. Converting an AnsiString to PAnsiChar and returning that will result in undefined behavior, it working on some platforms does not make it correct. You need to manually allocate the resulting PAnsiChar and copy over the content of the string and then provide some other function to free the PAnsiChar again. ---------------------------------------------------------------------- H₂SO₄ · 2025-10-28 05:54 There are (as far as I know) three common ways of handling the issue of passing heap allocations around ABI boundaries (aka between languages): Keep all allocations and deallocations within the libraryKeep all allocations and deallocations within the main program and use preallocated buffers for library functionsMake sure that the same memory manager is used on both sides of the boundary Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { #1: Returns string that must be freed using `Lib_Free` }function Lib_Bar(): PAnsiChar; cdecl;procedure Lib_Free(Block: Pointer); cdecl; { #2: Writes into `Buffer`, returns number of chars written, error code if `Size` is too small }function Lib_Bar(Buffer: PAnsiChar; Size: csize_t): csize_t; cdecl; { #3: Main program passes its own `malloc` and `free` as function pointers, which the library subsequently uses for its own allocations } type  PLibMalloc = function (Size: csize_t): Pointer; cdecl;  PLibFree = procedure (Block: Pointer); cdecl; var  ExternalMalloc: PLibMalloc;  ExternalFree: PLibFree; procedure Lib_SetMemoryManager(Malloc: PLibMalloc; Free: PLibFree); cdecl;function Lib_Bar(): PAnsiChar; cdecl;