What is the correct usage pattern for a TBytes variable? From my understanding, TBytes is not a class, but a "dynamic array of bytes". I'm not sure where memory gets allocated for it, when it is freed, and which is the best way to pass it from a producer to a consumer. I want my producer to create a TBytes instance, then pass it to a consumer. After this happens, the producer wants to reuse its TBytes member variable, content in the knowledge that the consumer will eventually return the memory to the system. If TBytes was an object, I wouldn't have any problem, but I'm not sure how the TBytes works in this scenario.
For example, in object A, I want to assemble some data into a TBytes array that is a member of object A. When that is complete, I then want to pass the TBytes array to another object B, which then becomes the owner of the data. Meanwhile, back in object A, I want to start assembling more data, reusing the TBytes member variable.
type
TClassA = class
private
FData: TBytes;
public
procedure AssembleInput(p: Pointer; n: Cardinal);
end;
TClassB = class
public
procedure ProcessData(d: TBytes);
end;
var
a: TClassA;
b: TClassB;
procedure TClassA.AssembleInput(p: Pointer; n: Cardinal);
begin
SetLength(FData, n);
Move(p^, FData, n); // Is this correct?
...
b.ProcessData(FData);
...
// Would it be legal to reuse FData now? Perhaps by copying new (different)
// data into it?
end;
procedure TClassB.ProcessData(d: TBytes);
begin
// B used the TBytes here. How does it free them?
SetLength(d, 0); // Does this free any dynamic memory behind the scenes?
end;
Thanks in advance!