[metal] Manage staging buffer refcount in command buffer lifetime This avoids the potential refcount mismatches due to that we can have command buffers created but never submitted and such.
diff --git a/experimental/metal/direct_command_buffer.m b/experimental/metal/direct_command_buffer.m index 1835259..45f1448 100644 --- a/experimental/metal/direct_command_buffer.m +++ b/experimental/metal/direct_command_buffer.m
@@ -381,6 +381,10 @@ *out_command_buffer = &command_buffer->base; + // Increase command buffer refcount in the shared staging buffer. We tie this to the command + // buffer's lifetime to avoid resource leak. + iree_hal_metal_staging_buffer_increase_refcount(staging_buffer); + IREE_TRACE_ZONE_END(z0); return status; } @@ -392,6 +396,10 @@ iree_hal_metal_command_buffer_reset(command_buffer); + // Decrease command buffer refcount in the shared staging buffer, and potentially reclaim + // resources. We tie this to the command buffer's lifetime to avoid resource leak. + iree_hal_metal_staging_buffer_decrease_refcount(command_buffer->staging_buffer); + [command_buffer->state.encoder_event release]; // -1 IREE_ASSERT_EQ(command_buffer->state.compute_encoder, nil); IREE_ASSERT_EQ(command_buffer->state.blit_encoder, nil);