Until someone writes proper string visualisers for the Delphi debugger…
Posted by jpluimers on 2017/10/31
A few tricks to write long strings to files when the Delphi debugger cuts them off (just because they like using 4k buffers internally);
TStringStream.Create(lRequestMessage).SaveToFile('c:\temp\temp.txt')
TIniFile.Create('c:\a.txt').WriteString('a','a',BigStringVar)
TFileStream.Create('c:\a.txt', fmCreate or fmShareDenyNone).WriteBuffer(Pointer(TEncoding.UTF8.GetBytes(BigStringVar))^,Length(TEncoding.UTF8.GetBytes(BigStringVar)))
They all work form the debug inspector, but they do leak memory. See comments below.
Via:
- [WayBack] I remember seeing a trick to write a string from the Debug Evaluate/Modify window to a file, but forgot where. – Jeroen Wiert Pluimers – Google+
- [WayBack] If my alzheimer doesn’t cheating on me there was a discussion about how to save things from evaluator. (Ctrl+F7). – Attila Kovacs – Google+
–jeroen
PS: Uwe Schuster on G+ and Twitter:
I am not sure if the buffer is not a little bit bigger, but still to small in recent versions. To quote myself “Cannot say it often enough – do use ReadProcessMemory for your visualizer” twitter.com – Uwe Schuster on Twitter and just build a simple visualizer.
Remy said
All three suggested workarounds are leaking memory/resources because they are not destroying the temp objects they create. They need to be wrapped in a try/finally. And in the case of the TFileStream.WriteBuffer() suggestion, calling TEncoding.GetBytes() twice for a long string is very inefficient and memory wasteful. Cache the result to a variable first. Or use TStreamWriter.Write(String) instead.
Remy said
Also, the TStringStream and TIniFile approaches are not encoding the text to UTF-8, so they are subject to potential data loss for non-ASCII characters. At least the TFileStream approach (better TStreamWriter) takes UTF-8 into account.
jpluimers said
I know. But they all work from the debug inspector. I’ll add a warning.
Geza said
TFile.WriteAllText(aPath, aContents)
jpluimers said
Thanks. Docs: http://docwiki.embarcadero.com/Libraries/en/System.IOUtils.TFile.WriteAllText
bascyBas said
Another easy way is to copy it to the clipboard: write a small procedure that doesnt not much more then clipboard.astext := text and you can use that in the evaluate/modify window