EllanJiang/GameFramework · error · GameFrameworkException

Buffer is invalid.

Error message

Buffer is invalid.

What it means

The LoadBinaryFromFileSystem(string, byte[]) overload throws this GameFrameworkException when the buffer argument is null. The buffer is the caller-allocated destination for the binary data, so a null buffer makes the read impossible. The asset-name check has already passed at this point; only the null buffer is the problem.

Solutions

  1. Allocate the buffer before the call, e.g. new byte[expectedSize].
  2. Assert buffer != null (and buffer.Length >= expected size) before calling LoadBinaryFromFileSystem.
  3. Fix the initialization/pooling path that left the buffer null.
  4. Use the overload that allocates and returns the byte[] itself if you don't need to reuse a buffer.

Example fix

// before
int read = resourceManager.LoadBinaryFromFileSystem(name, buffer); // buffer is null
// after
if (buffer == null)
{
    buffer = new byte[expectedSize];
}
int read = resourceManager.LoadBinaryFromFileSystem(name, buffer, 0, buffer.Length);
Defensive patterns

Strategy: validation

Validate before calling

if (buffer == null)
    buffer = new byte[expectedSize];
else if (buffer.Length < expectedSize)
    throw new InvalidOperationException($"Buffer too small: {buffer.Length} < {expectedSize}");

Type guard

bool IsUsableBuffer(byte[] buf) => buf != null && buf.Length > 0;

Try / catch

try
{
    bytesRead = resourceManager.LoadBinaryFromFileSystem(binaryAssetName, buffer);
}
catch (GameFrameworkException ex) when (ex.Message.Contains("Buffer"))
{
    buffer = new byte[expectedSize];
    bytesRead = resourceManager.LoadBinaryFromFileSystem(binaryAssetName, buffer);
}

Prevention

When it happens

Trigger: Calling LoadBinaryFromFileSystem(binaryAssetName, buffer) with buffer == null — e.g. an uninitialized array field, a helper method forwarding a null parameter, or a lazily-allocated buffer that was never allocated before the first read.

Common situations: Buffer sized from a header length that was never read; a pooled buffer returned null when the pool was empty; refactoring changed the buffer from a constructor-assigned field to one assigned later, but the load ran first.

Related errors


AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15). Data as JSON: /api/errors/fb866f10452a1347. Report an issue: GitHub.

Appendix: source

Thrown at GameFramework/Resource/ResourceManager.cs:1885

            return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName);
        }

        /// <summary>
        /// 从文件系统中加载二进制资源。
        /// </summary>
        /// <param name="binaryAssetName">要加载二进制资源的名称。</param>
        /// <param name="buffer">存储加载二进制资源的二进制流。</param>
        /// <returns>实际加载了多少字节。</returns>
        public int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer)
        {
            if (string.IsNullOrEmpty(binaryAssetName))
            {
                throw new GameFrameworkException("Binary asset name is invalid.");
            }

            if (buffer == null)
            {
                throw new GameFrameworkException("Buffer is invalid.");
            }

            return m_ResourceLoader.LoadBinaryFromFileSystem(binaryAssetName, buffer, 0, buffer.Length);
        }

        /// <summary>
        /// 从文件系统中加载二进制资源。
        /// </summary>
        /// <param name="binaryAssetName">要加载二进制资源的名称。</param>
        /// <param name="buffer">存储加载二进制资源的二进制流。</param>
        /// <param name="startIndex">存储加载二进制资源的二进制流的起始位置。</param>
        /// <returns>实际加载了多少字节。</returns>
        public int LoadBinaryFromFileSystem(string binaryAssetName, byte[] buffer, int startIndex)
        {
            if (string.IsNullOrEmpty(binaryAssetName))
            {
                throw new GameFrameworkException("Binary asset name is invalid.");
            }

View on GitHub (pinned to d0c010b051)