8000 No more NREs when disposing/finalizing, fixes #1547 by Kaoticz · Pull Request #1567 · DSharpPlus/DSharpPlus · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DSharpPlus.CommandsNext/CommandsNextExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ internal CommandsNextExtension(CommandsNextConfiguration cfg)
/// Disposes of this the resources used by CNext.
/// </summary>
public override void Dispose()
=> this.Config.CommandExecutor.Dispose();

~CommandsNextExtension()
{
this.Dispose();
this.Config.CommandExecutor.Dispose();

// Satisfy rule CA1816. Can be removed if this class is sealed.
GC.SuppressFinalize(this);
}

#region DiscordClient Registration
Expand Down
24 changes: 13 additions & 11 deletions DSharpPlus.Interactivity/EventHandling/EventWaiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,33 @@ private Task HandleEvent(DiscordClient client, T eventargs)
return Task.CompletedTask;
}

~EventWaiter()
{
this.Dispose();
}

/// <summary>
/// Disposes this EventWaiter
/// </summary>
public void Dispose()
{
if (this._disposed)
return;

this._disposed = true;
if (this._event != null)

if (this._event != null && this._handler != null)
this._event.Unregister(this._handler);

this._event = null;
this._handler = null;
this._client = null;
this._event = null!;
this._handler = null!;
this._client = null!;

if (this._matchrequests != null)
this._matchrequests.Clear();
if (this._collectrequests != null)
this._collectrequests.Clear();
< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker=" ">
this._matchrequests = null;
this._collectrequests = null;
this._matchrequests = null!;
this._collectrequests = null!;

// Satisfy rule CA1816. Can be removed if this class is sealed.
GC.SuppressFinalize(this);
}
}
}
23 changes: 12 additions & 11 deletions DSharpPlus.Interactivity/EventHandling/Paginator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,23 @@ private async Task PaginateAsync(IPaginationRequest p, DiscordEmoji emoji)
await builder.ModifyAsync(msg);
}

~Paginator()
{
this.Dispose();
}

/// <summary>
/// Disposes this EventWaiter
/// </summary>
public void Dispose()
{
this._client.MessageReactionAdded -= this.HandleReactionAdd;
this._client.MessageReactionRemoved -= this.HandleReactionRemove;
this._client.MessageReactionsCleared -= this.HandleReactionClear;
this._client = null;
this._requests.Clear();
this._requests = null;
// Why doesn't this class implement IDisposable?

if (this._client != null)
{
this._client.MessageReactionAdded -= this.HandleReactionAdd;
this._client.MessageReactionRemoved -= this.HandleReactionRemove;
this._client.MessageReactionsCleared -= this.HandleReactionClear;
this._client = null!;
}

this._requests?.Clear();
this._requests = null!;
}
}
}
26 changes: 15 additions & 11 deletions DSharpPlus.Interactivity/EventHandling/Poller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,26 @@ private Task HandleReactionClear(DiscordClient client, MessageReactionsClearEven
return Task.CompletedTask;
}

~Poller()
{
this.Dispose();
}

/// <summary>
/// Disposes this EventWaiter
/// </summary>
public void Dispose()
{
this._client.MessageReactionAdded -= this.HandleReactionAdd;
this._client.MessageReactionRemoved -= this.HandleReactionRemove;
this._client.MessageReactionsCleared -= this.HandleReactionClear;
this._client = null;
this._requests.Clear();
this._requests = null;
// Why doesn't this class implement IDisposable?

if (this._client != null)
{
this._client.MessageReactionAdded -= this.HandleReactionAdd;
this._client.MessageReactionRemoved -= this.HandleReactionRemove;
this._client.MessageReactionsCleared -= this.HandleReactionClear;
this._client = null!;
}

if (this._requests != null)
{
this._requests.Clear();
this._requests = null!;
}
}
}
}
46 changes: 23 additions & 23 deletions DSharpPlus.Interactivity/EventHandling/ReactionCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,34 @@ private Task HandleReactionClear(DiscordClient client, MessageReactionsClearEven
return Task.CompletedTask;
}

~ReactionCollector()
{
this.Dispose();
}

/// <summary>
/// Disposes this EventWaiter
/// </summary>
public void Dispose()
{
this._client = null;
this._client = null!;

if (this._reactionAddHandler != null)
this._reactionAddEvent?.Unregister(this._reactionAddHandler);

this._reactionAddEvent.Unregister(this._reactionAddHandler);
this._reactionRemoveEvent.Unregister(this._reactionRemoveHandler);
this._reactionClearEvent.Unregister(this._reactionClearHandler);
if (this._reactionRemoveHandler != null)
this._reactionRemoveEvent?.Unregister(this._reactionRemoveHandler);

this._reactionAddEvent = null;
this._reactionAddHandler = null;
this._reactionRemoveEvent = null;
this._reactionRemoveHandler = null;
this._reactionClearEvent = null;
this._reactionClearHandler = null;
if (this._reactionClearHandler != null)
this._reactionClearEvent?.Unregister(this._reactionClearHandler);

this._requests.Clear();
this._requests = null;
this._reactionAddEvent = null!;
this._reactionAddHandler = null!;
this._reactionRemoveEvent = null!;
this._reactionRemoveHandler = null!;
this._reactionClearEvent = null!;
this._reactionClearHandler = null!;

this._requests?.Clear();
this._requests = null!;

// Satisfy rule CA1816. Can be removed if this class is sealed.
GC.SuppressFinalize(this);
}
}

Expand All @@ -214,19 +217,16 @@ public ReactionCollectRequest(DiscordMessage msg, TimeSpan timeout)
this._ct.Token.Register(() => this._tcs.TrySetResult(null));
}

~ReactionCollectRequest()
{
this.Dispose();
}

public void Dispose()
{
GC.SuppressFinalize(this);
this._ct.Dispose();
this._tcs = null;
this._message = null;
this._collected?.Clear();
this._collected = null;

// Satisfy rule CA1816. Can be removed if this class is sealed.
GC.SuppressFinalize(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,19 @@ public CollectRequest(Func<T, bool> predicate, TimeSpan timeout)
this._collected = new ConcurrentHashSet<T>();
}

~CollectRequest()
{
this.Dispose();
}

/// <summary>
/// Disposes this CollectRequest.
/// </summary>
public void Dispose()
{
this._ct.Dispose();
this._tcs = null;
this._predicate = null;
this._tcs = null!;
this._predicate = null!;

if (this._collected != null)
{
this._collected.Clear();
this._collected = null;
this._collected = null!;
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions DSharpPlus.Interactivity/EventHandling/Requests/MatchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,17 @@ public MatchRequest(Func<T, bool> predicate, TimeSpan timeout)
this._timeout = timeout;
}

~MatchRequest()
{
this.Dispose();
}

/// <summary>
/// Disposes this MatchRequest.
/// </summary>
public void Dispose()
{
this._ct.Dispose();
this._tcs = null;
this._predicate = null;
this._ct?.Dispose();
this._tcs = null!;
this._predicate = null!;

// Satisfy rule CA1816. Can be removed if this class is sealed.
GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,15 @@ public async Task<TaskCompletionSource<bool>> GetTaskCompletionSourceAsync()
return this._tcs;
}

~PaginationRequest()
{
this.Dispose();
}

/// <summary>
/// Disposes this PaginationRequest.
/// </summary>
public void Dispose()
{
this._ct.Dispose();
this._tcs = null;
// Why doesn't this class implement IDisposable?

this._ct?.Dispose();
this._tcs = null!;
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions DSharpPlus.Interactivity/EventHandling/Requests/PollRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,15 @@ internal void AddReaction(DiscordEmoji emoji, DiscordUser member)
}
}

~PollRequest()
{
this.Dispose();
}

/// <summary>
/// Disposes this PollRequest.
/// </summary>
public void Dispose()
{
this._ct.Dispose();
this._tcs = null;
// Why doesn't this class implement IDisposable?

this._ct?.Dispose();
this._tcs = null!;
}
}

Expand Down
28 changes: 13 additions & 15 deletions DSharpPlus.Interactivity/InteractivityExtension.cs
< 6B28 tr data-hunk="4005d10a61ad42fc8b2db55190028adcf81df81959a167bb63112fe666929c30" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -1060,21 +1060,19 @@ private async Task HandleInvalidInteraction(DiscordInteraction interaction)

public override void Dispose()
{
this.ComponentEventWaiter.Dispose();
this.ModalEventWaiter.Dispose();
this.ReactionCollector.Dispose();
this.ComponentInteractionWaiter.Dispose();
this.MessageCreatedWaiter.Dispose();
this.MessageReactionAddWaiter.Dispose();
this.Paginator.Dispose();
this.Poller.Dispose();
this.TypingStartWaiter.Dispose();
this._compPaginator.Dispose();
}

~InteractivityExtension()
{
this.Dispose();
this.ComponentEventWaiter?.Dispose();
this.ModalEventWaiter?.Dispose();
this.ReactionCollector?.Dispose();
this.ComponentInteractionWaiter?.Dispose();
this.MessageCreatedWaiter?.Dispose();
this.MessageReactionAddWaiter?.Dispose();
this.Paginator?.Dispose();
this.Poller?.Dispose();
this.TypingStartWaiter?.Dispose();
this._compPaginator?.Dispose();

// Satisfy rule CA1816. Can be removed if this class is sealed.
GC.SuppressFinalize(this);
}
}
}
15 changes: 6 additions & 9 deletions DSharpPlus.Lavalink/LavalinkExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,19 @@ private Task Con_Disconnected(LavalinkNodeConnection node, NodeDisconnectedEvent

public override void Dispose()
{
foreach(var node in this._connectedNodes)
foreach (var node in this._connectedNodes)
{
// undoubtedly there will be some GitHub comments about this. Help.
node.Value.StopAsync().GetAwaiter().GetResult();
}
this._connectedNodes.Clear();

// unhook events
_nodeDisconnected.UnregisterAll();
this._connectedNodes?.Clear();

// Hi GC! <3 😘 clean me up uwu
}
// unhook events
this._nodeDisconnected?.UnregisterAll();

~LavalinkExtension()
{
this.Dispose();
// Satisfy rule CA1816. Can be removed if this class is sealed.
GC.SuppressFinalize(this);
}
}
}
2 changes: 1 addition & 1 deletion DSharpPlus.Rest/DiscordRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2293,7 +2293,7 @@ public override void Dispose()
return;
this._disposed = true;
this._guilds = null;
this.ApiClient._rest.Dispose();
this.ApiClient?._rest?.Dispose();
}
}
}
Loading
0