diff --git a/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs b/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs index f537c41..3557302 100644 --- a/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs +++ b/MQTTnet.Core/Adapter/MqttChannelCommunicationAdapter.cs @@ -139,9 +139,9 @@ namespace MQTTnet.Core.Adapter public async Task ReceivePacketAsync(TimeSpan timeout, CancellationToken cancellationToken) { + ReceivedMqttPacket receivedMqttPacket = null; try { - ReceivedMqttPacket receivedMqttPacket; if (timeout > TimeSpan.Zero) { receivedMqttPacket = await ReceiveAsync(_channel.ReceiveStream, cancellationToken).TimeoutAfter(timeout).ConfigureAwait(false); @@ -185,6 +185,10 @@ namespace MQTTnet.Core.Adapter { throw new MqttCommunicationException(exception); } + finally + { + receivedMqttPacket?.Dispose(); + } } private static async Task ReceiveAsync(Stream stream, CancellationToken cancellationToken) diff --git a/MQTTnet.Core/Adapter/ReceivedMqttPacket.cs b/MQTTnet.Core/Adapter/ReceivedMqttPacket.cs index f8adc2e..f7fdbcc 100644 --- a/MQTTnet.Core/Adapter/ReceivedMqttPacket.cs +++ b/MQTTnet.Core/Adapter/ReceivedMqttPacket.cs @@ -4,7 +4,7 @@ using MQTTnet.Core.Packets; namespace MQTTnet.Core.Adapter { - public class ReceivedMqttPacket + public sealed class ReceivedMqttPacket : IDisposable { public ReceivedMqttPacket(MqttPacketHeader header, MemoryStream body) { @@ -15,5 +15,10 @@ namespace MQTTnet.Core.Adapter public MqttPacketHeader Header { get; } public MemoryStream Body { get; } + + public void Dispose() + { + Body?.Dispose(); + } } } diff --git a/MQTTnet.Core/Serializer/MqttPacketWriter.cs b/MQTTnet.Core/Serializer/MqttPacketWriter.cs index fcd883c..4c86f96 100644 --- a/MQTTnet.Core/Serializer/MqttPacketWriter.cs +++ b/MQTTnet.Core/Serializer/MqttPacketWriter.cs @@ -9,7 +9,7 @@ namespace MQTTnet.Core.Serializer public sealed class MqttPacketWriter : BinaryWriter { public MqttPacketWriter(Stream stream) - : base(stream) + : base(stream, Encoding.UTF8, true) { }