{"record":{"id":"fc89b3d9cafef70f","repo":"MonoGame/MonoGame","slug":"ensure-that-the-count-is-greater-than-zero","errorCode":null,"errorMessage":"Ensure that the count is greater than zero.","messagePattern":"Ensure that the count is greater than zero\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Audio/SoundEffect.cs","lineNumber":175,"sourceCode":"        /// <param name=\"loopLength\">The duration of the sound data loop in samples.</param>\r\n        /// <remarks>This only supports uncompressed 16bit PCM wav data.</remarks>\r\n        public SoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioChannels channels, int loopStart, int loopLength)\r\n        {\r\n            Initialize();\r\n            if (_systemState != SoundSystemState.Initialized)\r\n                throw new NoAudioHardwareException(\"Audio has failed to initialize. Call SoundEffect.Initialize() before sound operation to get more specific errors.\");\r\n\r\n            if (sampleRate < 8000 || sampleRate > 48000)\r\n                throw new ArgumentOutOfRangeException(\"sampleRate\");\r\n            if ((int)channels != 1 && (int)channels != 2)\r\n                throw new ArgumentOutOfRangeException(\"channels\");\r\n\r\n            if (buffer == null || buffer.Length == 0)\r\n                throw new ArgumentException(\"Ensure that the buffer length is non-zero.\", \"buffer\");\r\n\r\n            var blockAlign = (int)channels * 2;\r\n            if (count <= 0)\r\n                throw new ArgumentException(\"Ensure that the count is greater than zero.\", \"count\");\r\n            if ((count % blockAlign) != 0)\r\n                throw new ArgumentException(\"Ensure that the count meets the block alignment requirements for the number of channels.\", \"count\");\r\n\r\n            if (offset < 0)\r\n                throw new ArgumentException(\"The offset cannot be negative.\", \"offset\");\r\n            if (((ulong)count + (ulong)offset) > (ulong)buffer.Length)\r\n                throw new ArgumentException(\"Ensure that the offset+count region lines within the buffer.\", \"offset\");\r\n\r\n            var totalSamples = count / blockAlign;\r\n\r\n            if (loopStart < 0)\r\n                throw new ArgumentException(\"The loopStart cannot be negative.\", \"loopStart\");\r\n            if (loopStart > totalSamples)\r\n                throw new ArgumentException(\"The loopStart cannot be greater than the total number of samples.\", \"loopStart\");\r\n\r\n            if (loopLength == 0)\r\n                loopLength = totalSamples - loopStart;\r\n\r","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Audio/SoundEffect.cs#L157-L193","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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."],"exampleFix":"// before\nint count = dataEnd - dataStart; // 0 when dataEnd == dataStart\nvar sfx = new SoundEffect(buf, dataStart, count, rate, ch, 0, 0);\n\n// after\nint count = dataEnd - dataStart;\nif (count <= 0)\n    throw new InvalidDataException($\"No PCM bytes (dataStart={dataStart}, dataEnd={dataEnd}).\");\nvar sfx = new SoundEffect(buf, dataStart, count, rate, ch, 0, 0);","handlingStrategy":"validation","validationCode":"int count = bytesAvailable;\nif (count <= 0) throw new InvalidDataException(\"No PCM bytes.\");\nvar sfx = new SoundEffect(buffer, offset, count, rate, ch, 0, 0);","typeGuard":"static bool HasPositiveCount(int count) => count > 0;","tryCatchPattern":"try { return new SoundEffect(buf, off, count, rate, ch, 0, 0); }\ncatch (ArgumentException ex) when (ex.ParamName == \"count\" && count <= 0)\n{ throw new InvalidDataException(\"Empty PCM region.\", ex); }","preventionTips":["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."],"tags":["audio","argument-validation","monogame","buffer"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}