quamotion/madb

The buffer length X does not match the expected buffer length for a picture of width W, height H and pixel depth D

seltix5 opened this issue · 2 comments

Hello,

I was working with nuget SharpAdbClient 2.2.41 without any problem and now after updating to current version (dowloaded master) I cant get screenshots.

Code:
System.Threading.Tasks.Task<Image> task = client.GetFrameBufferAsync(device, CancellationToken.None);

Error:
The buffer length 1048576 does not match the expected buffer length for a picture of width 640, height 360 and pixel depth 32

throw new ArgumentOutOfRangeException(nameof(buffer), $"The buffer length {buffer.Length} does not match the expected buffer " +
$"length for a picture of width {this.Width}, height {this.Height} and pixel depth {this.Bpp}");

(Using MEmu with android 5.1.1)

Any ideas?
Thanks!

After analyzing the old version I found the problem.
On the old version, this code :

// Optimization on .NET Core: Use the BufferPool to rent buffers
if (this.Data != null)
{
ArrayPool<byte>.Shared.Return(this.Data, clearArray: false);
}
this.Data = ArrayPool<byte>.Shared.Rent((int)this.Header.Size);

was like just this:
this.Data = new byte[this.Header.Size];

and the corresponding dispose code did not exist either :

if (this.Data != null)
{
ArrayPool<byte>.Shared.Return(this.Data, clearArray: false);
}

The problem here, I believe, its because the pool is no more created for the requested size, when the above buffer size check is done it may fail because the current buffer size can be bigger than the necessary since its now a pool and not a fixed array anymore.

What would be the best solution for this problem?

here is what I did to fix those problems : SharpAdb/AdvancedSharpAdbClient#3 (comment)