[SOLVED] Using New and Dispose properly? lazarus / 21-General ---------------------------------------------------------------------- Gizmo · 2021-03-26 16:36 I have a couple of areas where I think I am having memory issues; memory leaks, specifically, because memory usage keep going up and up even though the looping cycle is freeing everything (except I cant be sure about two areas). One of the areas where I suspect memory may be getting lost is given by the following type declaration : 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 PSrcInfo=^TSrcInfo;        TSrcInfo=packed record          nStructSize : DWord;          nBufSize : INT64;          pBuffer : LPVOID;        end;                   and then the following usage : 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 some procedure...var pSrcBuffer          : PSrcInfo;    MyStringData     : string;begin  MyStringData := 'some text from elsewhere';  New(pSrcBuffer);    pSrcBuffer.nStructSize := SizeOf(TSrcInfo);  // Should return 16 on 32-bit systems  pSrcBuffer.nBufSize    := Length(MyStringData);  pSrcBuffer.pBuffer     := @MyStringData[1];              // ... do stuff with pSrcBuffer      Dispose(pSrcBuffer);         end;  Question 1 : Is there a better\preferred way of creating such a data type besides using New? If so, what? Question 2 : New and Dispose are for dynamically allocated memory areas. I notice it does say that New should be initialised using FillWord or FillChar. However when I used FillWord, it compiled OK but an exception was thrown at execution. What is the right way to go about using FillWord in this context? Question 3 : Given all of the above, does it seem likely this is the source of my memory leak issues? Or does that appear unlikely? ---------------------------------------------------------------------- engkin · 2021-03-26 16:57 1-From what we see from your code, you don't need to use PSrcInfo, you can use TSrcInfo directly, and if needed pass its address: 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 var  SrcBuffer: TSrcInfo; 2-If you use TSrcInfo you initialize using Default: 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   SrcBuffer := Default(TSrcInfo);  SrcBuffer.nStructSize := SizeOf(TSrcInfo); 3-From the little code you show, this is not where the leak is. As for FillWord, you probably passed the wrong number to it. SizeOf is in bytes, not words. Stick with FillChar. ---------------------------------------------------------------------- ASerge · 2021-03-26 17:51 Quote from: Gizmo on March 26, 2021, 05:36:23 pmOr does that appear unlikely? Storing a pointer to string data is a bad idea. The string can be deleted later and the data will become invalid. In this particular case, the implementation will avoid reference counting and keep a copy of the initialization segment, but in general this is bad. Either store the string or allocate memory to copy it. ---------------------------------------------------------------------- Gizmo · 2021-03-26 19:28 OK, thanks guys. I appreciate your time and details as ever. Thanks so much