Creating a windows dll issues lazarus / 34-Windows ---------------------------------------------------------------------- lainz · 2023-07-20 00:26 Hi, I'm facing some issues with memory when making a dll Result of my function is a record, that's in memory until something removes it? 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   Taccesstoken = packed record    accesstoken: PChar;    userid: PChar;  end;   I create in the dll local variables 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 function somemethod: Taccesstoken; stdcall;var  accesstoken: ansistring = ''; ... accesstoken := Copy(somejson.asstring..., 1);result.accesstoken := PChar(accesstoken); Then I call the method 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 ...  public  rec1 = Tacesstoken; ... rec1 := somemethodindll; The problem is if I call other methods in the dll with the same PChar, sometimes memory is lost or modified. I think because the dll is no longer using it. To avoid this problem I make a copy of the PChars in the target application: 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 ...public...atoken = ansistring;... atoken :=rec1.accesstoken; My question is if there's a better way of doing this. ---------------------------------------------------------------------- KodeZwerg · 2023-07-20 05:12 AFAIK you need to allocate memory for the string, either in dll and provide there a free method or in your app code by passing also the length for the string. ---------------------------------------------------------------------- cdbc · 2023-07-20 05:28 Hi I came to the same conclusion as @KodeZwerg... You need something that's not refcounted, to base your pchar on... e.g.: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 function somemethod: Taccesstoken; stdcall;var  accesstoken: ansistring = ''; ... accesstoken := Copy(somejson.asstring..., 1);result.accesstoken := StrNew(PChar(accesstoken)); // alloc new mem// later, somewhere you free it again with StrDispose(rec.accesstoken) EDIT: Come to think of it, it's important, that you StrDispose in the same memory-manager as you StrNew it in, because they rely on the length being written just before the pointer itself. (...or You could just export StrDispose from the dll)  Another possibility is, (if it's feasible) to use a shortstring instead, it's a value-type: 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   Taccesstoken = packed record    accesstoken: shortstring;    userid: PChar;  end; Just a couple of ideas  Regards Benny ---------------------------------------------------------------------- Zvoni · 2023-07-20 06:54 Since it's windows: https://www.freepascal.org/docs-html/rtl/sharemem/index.html ---------------------------------------------------------------------- 440bx · 2023-07-20 12:34 Some generic advice that seems to apply in this case: 1. when using pointers, centralize memory management.  That is, either the app or the dll is responsible for managing memory but _not_ both, that is, do _not_ mix allocations and/or deallocations among them. 2. when using pointers,  ensure whatever space is needed is _clearly_ allocated by either the app or the dll (preferably the app, which should subsequently and eventually free the memory block.) corollary: if the memory management is partially done by the dll then the dll should export functions for the app to control when the allocation and de-allocation is done. HTH. ---------------------------------------------------------------------- lainz · 2023-07-20 12:47 Thanks to all =) I will use shortstring for now, but in the case I need longer strings, I will use StrNew (in the dll) and StrDispose (in the application, exported from the dll). ---------------------------------------------------------------------- KodeZwerg · 2023-07-20 12:56 Quote from: lainz on July 20, 2023, 02:47:38 pmThanks to all =) You are welcome. Quote from: lainz on July 20, 2023, 02:47:38 pmI will use StrNew (in the dll) and StrDispose (in the application, exported from the dll). Did you really read above messages? ---------------------------------------------------------------------- lainz · 2023-07-20 13:03 Quote from: KodeZwerg on July 20, 2023, 02:56:08 pmQuote from: lainz on July 20, 2023, 02:47:38 pmI will use StrNew (in the dll) and StrDispose (in the application, exported from the dll). Did you really read above messages? Yes I've readed. But I need to allocate in the dll better, since I don't know the size of the string previously, and also is a result, I don't know how to allocate the result of the record in my side... And I can't free in the dll since I need to use the value in the application. So I will free from the application with the StrDispose from the dll. Like said here: Quotecorollary: if the memory management is partially done by the dll then the dll should export functions for the app to control when the allocation and de-allocation is done. Quote(...or You could just export StrDispose from the dll) The allocation will be done in the function, and de allocation in the method StrDispose exported. ---------------------------------------------------------------------- cdbc · 2023-07-20 16:38 Hi You are most welcome, have fun Regards Benny