#1 Yesterday 12:37:02

tbo
Member
Registered: 2015-04-20
Posts: 354

Inconsistency TTextWriter.SetText

mORMot2, Version 2.3.8840, Delphi 12.1, 32Bit

Function TTextWriter.SetText does not behave consistently. The result for case 1 and case 2 are different.

var
  w: TJsonWriter;
  b: TTextWriterStackBuffer;
  m: TMemoryStream;
  s: RawUtf8;
begin
  // 1) Case TTextWriterStackBuffer
  w := TJsonWriter.CreateOwnedStream(b);
  try
    w.Add(True);
    w.AddComma;
    w.SetText(s);
    w.Add(64);
    w.SetText(s);
    ShowMessage(Utf8ToString(s));  // Result: 64
  finally
    w.Free;
  end;

  // 2) Case TMemoryStream
  m := TMemoryStream.Create;
  try
    w := TJsonWriter.Create(m);
    try
      w.Add(True);
      w.AddComma;
      w.SetText(s);
      w.Add(64);
      w.SetText(s);
      ShowMessage(Utf8ToString(s));  // Result: true,64
    finally
      w.Free;
    end;
  finally
    m.Free;
  end;

The behaviour differs depending on the buffer type and I can't find a description for it.

With best regards
Thomas

Offline

#2 Yesterday 16:03:33

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,713
Website

Re: Inconsistency TTextWriter.SetText

This inconsistency comes clearly from a wrong usage of the methods.

Once you use SetText or FlushFinal, the TTextWriter is in an undetermined state.
You need to call CancelAll to reuse the instance.

I have made this more clear in the documentation:
https://github.com/synopse/mORMot2/commit/89411ef4

Offline

Board footer

Powered by FluxBB