MonoGame/MonoGame · error · ArgumentException
Ensure that the count is greater than zero.
Error message
Ensure that the count is greater than zero.
What it means
Thrown as ArgumentException (paramName 'count') from the public SoundEffect constructor when count <= 0. `count` is the byte length of PCM to use starting at `offset`; zero or negative bytes means no samples and no playable sound. The check fires after the buffer non-empty check and before the block-alignment check.
Source
Thrown at MonoGame.Framework/Audio/SoundEffect.cs:175
/// <param name="loopLength">The duration of the sound data loop in samples.</param>
/// <remarks>This only supports uncompressed 16bit PCM wav data.</remarks>
public SoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength)
{
Initialize();
if (_systemState != SoundSystemState.Initialized)
throw new NoAudioHardwareException("Audio has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.");
if (sampleRate < 8000 || sampleRate > 48000)
throw new ArgumentOutOfRangeException("sampleRate");
if ((int)channels != 1 && (int)channels != 2)
throw new ArgumentOutOfRangeException("channels");
if (buffer == null || buffer.Length == 0)
throw new ArgumentException("Ensure that the buffer length is non-zero.", "buffer");
var blockAlign = (int)channels * 2;
if (count <= 0)
throw new ArgumentException("Ensure that the count is greater than zero.", "count");
if ((count % blockAlign) != 0)
throw new ArgumentException("Ensure that the count meets the block alignment requirements for the number of channels.", "count");
if (offset < 0)
throw new ArgumentException("The offset cannot be negative.", "offset");
if (((ulong)count + (ulong)offset) > (ulong)buffer.Length)
throw new ArgumentException("Ensure that the offset+count region lines within the buffer.", "offset");
var totalSamples = count / blockAlign;
if (loopStart < 0)
throw new ArgumentException("The loopStart cannot be negative.", "loopStart");
if (loopStart > totalSamples)
throw new ArgumentException("The loopStart cannot be greater than the total number of samples.", "loopStart");
if (loopLength == 0)
loopLength = totalSamples - loopStart;
View on GitHub (pinned to 1d71bbd0ff)
Solutions
- Verify count > 0 at the loader before constructing the SoundEffect; refuse corrupt sources.
- Recompute count from the actual decoded byte range, not from header fields.
- If the source genuinely has no audio, skip construction rather than passing count=0.
- Add a debug assertion on count to localize where the zero/negative originates.
Example fix
// before
int count = dataEnd - dataStart; // 0 when dataEnd == dataStart
var sfx = new SoundEffect(buf, dataStart, count, rate, ch, 0, 0);
// after
int count = dataEnd - dataStart;
if (count <= 0)
throw new InvalidDataException($"No PCM bytes (dataStart={dataStart}, dataEnd={dataEnd}).");
var sfx = new SoundEffect(buf, dataStart, count, rate, ch, 0, 0); Defensive patterns
Strategy: validation
Validate before calling
int count = bytesAvailable;
if (count <= 0) throw new InvalidDataException("No PCM bytes.");
var sfx = new SoundEffect(buffer, offset, count, rate, ch, 0, 0); Type guard
static bool HasPositiveCount(int count) => count > 0;
Try / catch
try { return new SoundEffect(buf, off, count, rate, ch, 0, 0); }
catch (ArgumentException ex) when (ex.ParamName == "count" && count <= 0)
{ throw new InvalidDataException("Empty PCM region.", ex); } Prevention
- Derive count from the actual decoded byte range, not header fields.
- Refuse to construct when the source produced 0 bytes.
- Add a debug assertion on count to localize the underflow.
When it happens
Trigger: Passing count=0 (e.g., when bytesAvailable computed as 0), a negative count from an underflowed arithmetic expression, or forwarding a decoder's 'bytes read' value of 0.
Common situations: Decoding an empty/truncated WAV body; off-by-one or inverted range math (end - start where end < start); forwarding a length field read from a corrupt header that is 0; unit conversion losing magnitude (samples vs bytes).
Related errors
- Ensure that the buffer length is non-zero.
- The offset cannot be negative.
- Number of bytes must be greater than zero.
- Buffer is shorter than the specified number of bytes from th
- Number of bytes does not match format alignment.
AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13).
Data as JSON: /api/errors/fc89b3d9cafef70f.
Report an issue: GitHub.