diff --git a/src/Dapper.AOT/Internal/AsyncCommandState.cs b/src/Dapper.AOT/Internal/AsyncCommandState.cs index 4922de8b..ae3e8086 100644 --- a/src/Dapper.AOT/Internal/AsyncCommandState.cs +++ b/src/Dapper.AOT/Internal/AsyncCommandState.cs @@ -123,6 +123,8 @@ public virtual ValueTask DisposeAsync() if (cmd is not null) { + // match vanilla's teardown: release caller-supplied parameter objects + cmd.Parameters.Clear(); if (conn is not null && (_flags & FLAG_CLOSE_CONNECTION) != 0) { // need to close the connection and dispose the command @@ -178,7 +180,12 @@ public virtual void Dispose() { var cmd = Command; Command = null; - cmd?.Dispose(); + if (cmd is not null) + { + // match vanilla's teardown: release caller-supplied parameter objects + cmd.Parameters.Clear(); + cmd.Dispose(); + } var conn = connection; connection = null; diff --git a/src/Dapper.AOT/Internal/SyncCommandState.cs b/src/Dapper.AOT/Internal/SyncCommandState.cs index 90674cbb..5ff39a72 100644 --- a/src/Dapper.AOT/Internal/SyncCommandState.cs +++ b/src/Dapper.AOT/Internal/SyncCommandState.cs @@ -62,7 +62,12 @@ public void Dispose() { var cmd = Command; Command = null; - cmd?.Dispose(); + if (cmd is not null) + { + // match vanilla's teardown: release caller-supplied parameter objects + cmd.Parameters.Clear(); + cmd.Dispose(); + } var conn = connection; connection = null; diff --git a/src/Dapper.AOT/UnifiedCommand.cs b/src/Dapper.AOT/UnifiedCommand.cs index f7b69500..6744fbad 100644 --- a/src/Dapper.AOT/UnifiedCommand.cs +++ b/src/Dapper.AOT/UnifiedCommand.cs @@ -219,7 +219,14 @@ private DbCommand UnsafeWithCommandForParameters() internal void Cleanup() { - dbCommand?.Dispose(); + if (dbCommand is not null) + { + // match vanilla Dapper's teardown: a parameter object the caller supplied + // (ICustomQueryParameter, DynamicParameters.Add(DbParameter), etc) must not + // stay owned by a dead command's collection, or it cannot be reused + dbCommand.Parameters.Clear(); + dbCommand.Dispose(); + } #if NET6_0_OR_GREATER batch?.Dispose(); #endif