How does one go about, clearing a memorystream, without 'Free'ing' it?!?
TMemoryStream has a
Clear() method:
Clear sets the position and size to 0, and sets the capacity of the stream to 0, thus freeing all memory allocated for the stream.
Unfortunately, you cannot clear the stream's internal buffer while leaving its
Capacity as-is. Setting the
Size property to 0 also sets the
Capacity property to 0, freeing the buffer. One would think that shrinking the
Size would leave the
Capacity at its current value, but that is not actually the case. Setting a new
Size sets a new
Capacity as well, so the
Capacity will always be
at least the
Size (it may be higher, due to the algorithm used to grow the buffer in incremental steps, but that does not apply when setting the new
Size/
Capacity to 0).
If you don't want to free the buffer, then don't use the
Size property. Reset the
Position property to 0, write as needed, and use the
Position rather than the
Size to know how many bytes are in the stream.