Skip to content

Commit

Permalink
Add using where needed to dispose objects
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Oct 19, 2023
1 parent 76f0b9a commit a371559
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ dotnet_style_prefer_is_null_check_over_reference_equality_method = false:silent

dotnet_style_predefined_type_for_locals_parameters_members = true:silent
dotnet_style_predefined_type_for_member_access = true:silent

# CA2000: Dispose objects before losing scope
dotnet_diagnostic.CA2000.severity = suggestion
4 changes: 2 additions & 2 deletions Samples/1b.QrCodeAuthentication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ void DrawQRCode( QrAuthSession authSession )
Console.WriteLine();

// Encode the link as a QR code
var qrGenerator = new QRCodeGenerator();
using var qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode( authSession.ChallengeURL, QRCodeGenerator.ECCLevel.L );
var qrCode = new AsciiQRCode( qrCodeData );
using var qrCode = new AsciiQRCode( qrCodeData );
var qrCodeAsAsciiArt = qrCode.GetGraphic( 1, drawQuietZones: false );

Console.WriteLine( "Use the Steam Mobile App to sign in via QR code:" );
Expand Down
7 changes: 4 additions & 3 deletions SteamKit2/SteamKit2/Networking/Steam3/UdpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public void Send( byte[] data )
if ( state != (int)State.Connected )
return;

SendData( new MemoryStream( data ) );
using var ms = new MemoryStream( data );
SendData( ms );
}

/// <summary>
Expand Down Expand Up @@ -355,7 +356,7 @@ private bool DispatchMessage()
if ( numPackets == 0 )
return false;

MemoryStream payload = new MemoryStream();
using MemoryStream payload = new MemoryStream();
for ( uint i = 0; i < numPackets; i++ )
{
var handled = inPackets.TryGetValue(++inSeqHandled, out var packet);
Expand Down Expand Up @@ -431,7 +432,7 @@ private void NetLoop(object? param)
// Data from the desired server was received; delay timeout
timeOut = DateTime.Now.AddSeconds(TIMEOUT_DELAY);

MemoryStream ms = new MemoryStream(buf, 0, length);
using MemoryStream ms = new MemoryStream(buf, 0, length);
UdpPacket packet = new UdpPacket(ms);

ReceivePacket(packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using SteamKit2.Internal;

namespace SteamKit2
Expand Down Expand Up @@ -112,7 +111,9 @@ public IDisposable Subscribe<TCallback>( JobID jobID, Action<TCallback> callback
throw new ArgumentNullException( nameof(callbackFunc) );
}

#pragma warning disable CA2000 // Not implicitly disposed
var callback = new Internal.Callback<TCallback>( callbackFunc, this, jobID );
#pragma warning restore CA2000
return new Subscription( callback, this );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static class ContentServerDirectoryService
throw new ArgumentNullException( nameof( configuration ) );
}

var directory = configuration.GetAsyncWebAPIInterface( "IContentServerDirectoryService" );
using var directory = configuration.GetAsyncWebAPIInterface( "IContentServerDirectoryService" );
var args = new Dictionary<string, object?>();

if ( cellId.HasValue )
Expand Down
2 changes: 1 addition & 1 deletion SteamKit2/SteamKit2/Steam/WebAPI/SteamDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static async Task<IReadOnlyCollection<ServerRecord>> LoadCoreAsync( SteamConfigu
throw new ArgumentNullException( nameof(configuration) );
}

var directory = configuration.GetAsyncWebAPIInterface( "ISteamDirectory" );
using var directory = configuration.GetAsyncWebAPIInterface( "ISteamDirectory" );
var args = new Dictionary<string, object?>
{
["cellid"] = configuration.CellID.ToString( CultureInfo.InvariantCulture )
Expand Down
2 changes: 1 addition & 1 deletion SteamKit2/SteamKit2/Steam/WebAPI/WebAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ private async Task<HttpResponseMessage> CallAsyncInternal( HttpMethod method, st
}
}

var request = new HttpRequestMessage( method, urlBuilder.ToString() );
using var request = new HttpRequestMessage( method, urlBuilder.ToString() );

if ( !paramsGoInQueryString )
{
Expand Down
5 changes: 2 additions & 3 deletions SteamKit2/SteamKit2/Util/HardwareUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ sealed class WindowsMachineInfoProvider : IMachineInfoProvider
{
public byte[]? GetMachineGuid()
{
var localKey = RegistryKey
.OpenBaseKey( Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64 )
.OpenSubKey( @"SOFTWARE\Microsoft\Cryptography" );
using var baseKey = RegistryKey.OpenBaseKey( Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64 );
using var localKey = baseKey.OpenSubKey( @"SOFTWARE\Microsoft\Cryptography" );

if ( localKey == null )
{
Expand Down

0 comments on commit a371559

Please sign in to comment.