[AudioToolbox] Fix computing array size. (#19952)

When resizing an array to a certain number of elements, we need to compute the
number of elements and use that to resize the array (and not use the size of
each element as the number of elements).

We also need to recompute the number of elements after calling the P/Invoke
(which may have changed the total size) - otherwise resizing the array is
entirely redundant (because the number of elements wouldn't change).
This commit is contained in:
Rolf Bjarne Kvinge 2024-01-30 14:26:20 +01:00 коммит произвёл GitHub
Родитель e9ae5ef996
Коммит 8e516381c3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 17 добавлений и 2 удалений

Просмотреть файл

@ -653,13 +653,15 @@ namespace AudioToolbox {
return null;
var elementSize = sizeof (AudioFormatType);
var data = new AudioFormatType [size / elementSize];
var elementCount = size / elementSize;
var data = new AudioFormatType [elementCount];
fixed (AudioFormatType* ptr = data) {
var res = AudioFormatPropertyNative.AudioFormatGetProperty (prop, 0, IntPtr.Zero, ref size, (IntPtr) ptr);
if (res != 0)
return null;
Array.Resize (ref data, elementSize);
elementCount = size / elementSize;
Array.Resize (ref data, elementCount);
return data;
}
}

Просмотреть файл

@ -4,6 +4,7 @@
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
@ -21,6 +22,18 @@ namespace MonoTouchFixtures.AudioToolbox {
[Preserve (AllMembers = true)]
public class AudioConverterTest {
[Test]
public void Formats ()
{
var decodeFormats = AudioConverter.DecodeFormats;
Assert.NotNull (decodeFormats, "Decode #1");
Assert.That (decodeFormats.Length, Is.GreaterThan (10), "Decode Length #1");
var encodeFormats = AudioConverter.EncodeFormats;
Assert.NotNull (encodeFormats, "Encode #1");
Assert.That (encodeFormats.Length, Is.GreaterThan (10), "Encode Length #1");
}
[Test]
public void Convert ()
{