TsgcWebTransportSession › Methods › RemoveStream

RemoveStream Method

Unregisters a stream from the session's list of open streams.

Syntax

procedure RemoveStream(const aStream: TsgcWebTransportStream);

Parameters

NameTypeDescription
aStreamconst TsgcWebTransportStreamThe stream to remove. Passing nil is safe and does nothing. Passing a stream that belongs to a different session, or one already removed, also does nothing.

Remarks

You rarely need to call this yourself. TsgcWebTransportStream.Close calls it for you, so a stream unregisters itself as soon as it closes. It is what makes StreamCount go down, and after it runs FindStreamById no longer returns that stream.

It does not free the stream object. The stream is unlinked from the list of open streams and moved to an internal list of closed streams, which owns it and releases it when the session is destroyed. Exactly one list owns a stream at any moment, so it is freed exactly once and never leaks. aStream therefore stays valid after the call, and so does every reference your code and the HTTP/3 demux still hold. It is safe to call from inside that stream's own OnData or OnClose handler.

Closed streams are only released when the session goes away, so a long-lived session that opens streams continuously still accumulates the closed objects, bounded by the lifetime of the QUIC connection. This is a deliberate trade: keeping the objects alive is what lets the pointers you hold stay safe. If the volume matters for your workload, prefer to open fewer, longer-lived streams.

RemoveStream does not close the underlying QUIC stream, does not raise OnClose, and does not notify the peer. It is a bookkeeping call. Call Close on the stream instead when your handlers need to know, since that both raises OnClose and unregisters the stream.

The operation is guarded by the session's critical section, so it is safe to call while another thread reads StreamCount or calls FindStreamById. Another thread holding the same stream pointer is safe too, since nothing is freed here.

This member is currently available in the Delphi/C++Builder implementation only. The .NET class has an internal RemoveStream(long streamId), keyed by id rather than by object and not callable from application code; the garbage collector handles the lifetime there, so no freeing is involved.

Example


// Finishing with a stream on a long-lived session.
procedure TfrmClient.FinishRequest(const aStream: TsgcWebTransportStream);
var
  oSession: TsgcWebTransportSession;
begin
  oSession := aStream.Session;

  // Close is enough: it unregisters the stream (calling RemoveStream for us)
  // and then raises OnClose. Calling RemoveStream here as well would be
  // redundant. The stream object stays valid either way.
  aStream.Close;

  memoLog.Lines.Add('streams still open: ' +
    IntToStr(oSession.StreamCount));
end;

Back to Methods