Lifetime and release of generated Runtime objects
Documentation status: architecture — see Maturity and evidence.
Generated .NET objects represent Runtime identities. The managed wrapper and the underlying Runtime object therefore have related but distinct lifetimes.
Deterministic release
When a generated object owns a releasable Runtime reference, application code should release it deterministically, preferably through the generated IDisposable/disposal pattern when that binding surface provides it.
using var session = runtime.CreateSession();
The generated binding translates disposal into the corresponding Runtime release operation. Application code should not call native Free on object handles.
Local references
For local execution, a generated object can wrap an opaque native Runtime handle. Release invalidates the owned Runtime reference according to the ABI ownership contract.
Temporary native buffers used to marshal strings or arrays are a separate concern from object release.
Remote references
For RPC execution, the equivalent object identity is represented by a remote InstanceId associated with type metadata and a session/lease. Disposal/release tells the remote Runtime that the client no longer needs that reference.
The application-facing object model should remain the same even though the transport representation differs.
Borrowed versus owned references
Not every reference returned by a Runtime call necessarily transfers ownership. Generated code must follow the published ownership metadata rather than disposing every encountered handle indiscriminately.
A binding should make owned lifetimes easy to release and borrowed lifetimes difficult to release incorrectly.
Finalizers are not the primary contract
Garbage collection can be a safety net, but deterministic Runtime resources should not rely on nondeterministic finalization as their normal lifecycle. This is especially important for remote leases, large resident models, files, sessions and other scarce resources.
After disposal
A disposed generated object should no longer be used for Runtime calls. Bindings may detect this locally or receive a stale/invalid-reference error from the Runtime adapter.