DLL Problem lazarus / 62-General ---------------------------------------------------------------------- cappe · 2017-03-19 17:23 I have made a simple DLL with lazarus 1.6.2 with 2 function: function GetValue : PAnsiChar; cdecl; var   aStr : String;   Temp : PAnsiChar; begin    aStr := 'wewewewewewe';    Result := StrAlloc(Length(aStr));    Temp := Addr(aStr[1]);    StrCopy(Result, Temp); end; procedure FreeString(strin: PAnsiChar); cdecl; begin   StrDispose(strin); end;  When I call this function from host program, after some call to these functions, the program closes by itself. Can anyone tell me why? How can I find out why? How can I fix the problem? Thank you ---------------------------------------------------------------------- ASerge · 2017-03-19 19:10 StrAlloc allocates LENGTH, but StrCopy copies LENGTH + 1 (+ the last zero) ---------------------------------------------------------------------- cappe · 2017-03-20 09:14 Quote from: ASerge on March 19, 2017, 08:10:37 pmStrAlloc allocates LENGTH, but StrCopy copies LENGTH + 1 (+ the last zero) I tried to change the code, but the program stops after a few calls to the dll. How can I fix the problem? ---------------------------------------------------------------------- ASerge · 2017-03-20 17:07 Quote from: cappe on March 20, 2017, 10:14:45 amHow can I fix the problem? The error in another place, as the saying goes, in line 13 Lib: 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 library ProjectLib; uses SysUtils; function GetValue: PAnsiChar; cdecl;var  AStr: string;begin  AStr := 'wewewewewewe';  Result := StrAlloc(Length(AStr) + Length(#0));  StrPCopy(Result, AStr);end; procedure FreeString(StrIn: PAnsiChar); cdecl;begin  StrDispose(StrIn);end; exports  GetValue, FreeString; beginend. Test (no error): 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 program ProjectTestLib; function GetValue: PAnsiChar; cdecl; external 'ProjectLib.dll';procedure FreeString(StrIn: PAnsiChar); cdecl; external 'ProjectLib.dll'; procedure Test;var  P: PAnsiChar;  i: Integer;begin  for i := 1 to 10000 do  begin    P := GetValue;    FreeString(P);  end;end; begin  Test;end. ---------------------------------------------------------------------- cappe · 2017-03-21 15:25 many many thanks ---------------------------------------------------------------------- ASerge · 2017-03-21 15:47 Quote from: cappe on March 21, 2017, 04:25:07 pmmany many thanks  Found error?