Skip to content

Commit 4cd0ea2

Browse files
committed
Fixed IDisposable implementation
1 parent e1df503 commit 4cd0ea2

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Fixed
9+
- IDisposable implementation.
10+
711
## [2.2.0] - 2022-11-12
812
### Changed
913
- .NET 6.0 target to .NET Standard 2.0.

PL.Modbus/Client.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace PL.Modbus
1313
/// </summary>
1414
public class Client : IDisposable
1515
{
16+
private bool _disposed = false;
1617
private readonly Protocol _protocol;
1718
private readonly byte _stationAddress;
1819

@@ -76,10 +77,21 @@ public int ReadTimeout
7677

7778
public void Dispose()
7879
{
79-
_stream.Close();
80+
Dispose(true);
8081
GC.SuppressFinalize(this);
8182
}
8283

84+
protected virtual void Dispose(bool disposing)
85+
{
86+
if (_disposed)
87+
return;
88+
89+
if (disposing)
90+
_stream.Dispose();
91+
92+
_disposed = true;
93+
}
94+
8395
/// <summary>
8496
/// Executes Modbus command.
8597
/// </summary>

PL.Modbus/PL.Modbus.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<RepositoryUrl>https://github.com/plasmapper/modbus-dotnet</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
1313
<PackageTags>modbus;client</PackageTags>
14-
<PackageReleaseNotes>Changed
15-
- .NET 6.0 target to .NET Standard 2.0.</PackageReleaseNotes>
14+
<PackageReleaseNotes>Fixed
15+
- IDisposable implementation</PackageReleaseNotes>
1616
<PackageLicenseExpression>MIT</PackageLicenseExpression>
17-
<VersionPrefix>2.2.0</VersionPrefix>
17+
<VersionPrefix>2.2.1</VersionPrefix>
1818
</PropertyGroup>
1919

2020
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

PL.Modbus/Stream.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,33 @@
33

44
namespace PL.Modbus
55
{
6-
internal abstract class Stream
6+
internal abstract class Stream : IDisposable
77
{
8+
private bool _disposed = false;
9+
810
public int ConnectTimeout { get; set; } = 1000;
911

1012
public int ReadTimeout { get; set; } = 300;
1113

1214
public int DelayBeforeWrite { get; set; } = 0;
1315

16+
public void Dispose()
17+
{
18+
Dispose(true);
19+
GC.SuppressFinalize(this);
20+
}
21+
22+
protected virtual void Dispose(bool disposing)
23+
{
24+
if (_disposed)
25+
return;
26+
27+
if (disposing)
28+
Close();
29+
30+
_disposed = true;
31+
}
32+
1433
public abstract byte[] Read(int byteCount);
1534

1635
public byte[] ReadTo(byte termByte)

0 commit comments

Comments
 (0)