feature: Implement Dispose for types which have disposable data (#1171)
* Initial set of dispose implementations Not handled yet: - Discord.Net.Websocket/Entities/SocketGuild - Discord.Net.Tests * Refactor DiscordSocketClient init into ctor This way we remove an IDisposableAnalyzer warning for not disposing the client when we set the client variable. * Dispose of clients when disposing sharded client * Finish implementing IDisposable where appropriate I opted to use NoWarn in the Tests project as it wasn't really necessary considering that our tests only run once * Tweak samples after feedback
This commit is contained in:
@@ -27,7 +27,7 @@ namespace Discord.Commands
|
||||
/// been successfully executed.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public class CommandService
|
||||
public class CommandService : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Occurs when a command-related information is received.
|
||||
@@ -67,6 +67,8 @@ namespace Discord.Commands
|
||||
internal readonly LogManager _logManager;
|
||||
internal readonly IReadOnlyDictionary<char, char> _quotationMarkAliasMap;
|
||||
|
||||
internal bool _isDisposed;
|
||||
|
||||
/// <summary>
|
||||
/// Represents all modules loaded within <see cref="CommandService"/>.
|
||||
/// </summary>
|
||||
@@ -330,9 +332,9 @@ namespace Discord.Commands
|
||||
|
||||
//Type Readers
|
||||
/// <summary>
|
||||
/// Adds a custom <see cref="TypeReader" /> to this <see cref="CommandService" /> for the supplied object
|
||||
/// Adds a custom <see cref="TypeReader" /> to this <see cref="CommandService" /> for the supplied object
|
||||
/// type.
|
||||
/// If <typeparamref name="T" /> is a <see cref="ValueType" />, a nullable <see cref="TypeReader" /> will
|
||||
/// If <typeparamref name="T" /> is a <see cref="ValueType" />, a nullable <see cref="TypeReader" /> will
|
||||
/// also be added.
|
||||
/// If a default <see cref="TypeReader" /> exists for <typeparamref name="T" />, a warning will be logged
|
||||
/// and the default <see cref="TypeReader" /> will be replaced.
|
||||
@@ -607,5 +609,23 @@ namespace Discord.Commands
|
||||
await _commandExecutedEvent.InvokeAsync(chosenOverload.Key.Command, context, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_moduleLock?.Dispose();
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
void IDisposable.Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user