zeromemory string state question - Printable Version +- Atozed Forums (https://www.atozed.com/forums) +-- Forum: Delphi (https://www.atozed.com/forums/forum-10.html) +--- Forum: English (https://www.atozed.com/forums/forum-13.html) +---- Forum: Delphi General Discussion (https://www.atozed.com/forums/forum-11.html) +---- Thread: zeromemory string state question (/thread-613.html) |
zeromemory string state question - Madammar - 08-27-2018 why some times when i do zeromemory for long string it does not get cleared from memory i do something like some times when i click on button 4 or 5 times i can see this text in memory viewer Code: procedure TForm1.OnClick(Sender: TObject); RE: zeromemory string state question - kudzu - 08-27-2018 Which compiler? D2009+ is Unicode. Pre 2009 strings are Ansi. RE: zeromemory string state question - rlebeau - 08-27-2018 (08-27-2018, 12:39 AM)Madammar Wrote: i do something like some times when i click on button 4 or 5 times i can see this text in memory viewer Such code is VERY dangerous the way you have written it. A String contains reference counted data, so if you have multiple String variables referencing the same physical data, you end up wiping out all of the variables together. But more importantly, in your example, pstr is pointing at a string literal, so it has a reference count of -1, and you end up trying to zero out read-only memory! In both cases, you need to call UniqueString() to ensure that pstr is the only variable referencing the data, and to ensure the data is writable: Code: procedure TForm1.OnClick(Sender: TObject); (08-27-2018, 02:53 PM)kudzu Wrote: Which compiler? D2009+ is Unicode. Pre 2009 strings are Ansi. Doesn't matter in this example, the code is using the generic String and Char types, so it would be calculating the correct byte length in all versions. RE: zeromemory string state question - Madammar - 08-27-2018 thank you very much Remy for that effort and all of this data kudzo i used delplhi berlin RE: zeromemory string state question - kudzu - 08-28-2018 "Doesn't matter in this example" But it helps to know in most cases what we are dealing with. |