EllanJiang/GameFramework · error · GameFrameworkException
Ensure size is invalid.
Error message
Ensure size is invalid.
What it means
DataProvider.EnsureCachedBytesSize throws this GameFrameworkException when asked to ensure a negative buffer size. The static byte cache used to hold binary data cannot be sized below zero, so the argument is rejected immediately. It is an argument validation guard before any memory allocation.
Solutions
- Check the value passed to EnsureCachedBytesSize; never pass a negative int.
- If triggered via ReadData, verify the resource's binary length metadata on disk is intact and re-build/export the resource package.
- Upgrade/re-sync GameFramework and resource version files so GetBinaryLength cannot report a bogus negative length.
Example fix
// before
DataProvider.EnsureCachedBytesSize(dataLength); // dataLength == -1
// after
if (dataLength > 0) { DataProvider.EnsureCachedBytesSize(dataLength); } Defensive patterns
Strategy: validation
Validate before calling
if (ensureSize < 0) throw new ArgumentOutOfRangeException(nameof(ensureSize)); DataProvider.EnsureCachedBytesSize(ensureSize);
Type guard
bool IsValidSize(int size) => size >= 0;
Prevention
- Never pass sentinel negatives (e.g. -1) as sizes.
- Sanity-check GetBinaryLength results before using them as sizes.
When it happens
Trigger: Calling DataProvider.EnsureCachedBytesSize with a negative int, directly or indirectly via ReadData when m_ResourceManager.GetBinaryLength(dataAssetName) returns a negative length (e.g. a corrupted resource index).
Common situations: Corrupt or stale resource metadata making GetBinaryLength return -1; a typo passing a sentinel value like -1 as ensureSize; custom code pre-sizing the cache before a load with a computed length that underflowed.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Verify resource count per frame is invalid.
- You must set resource manager first.
- You must set data provider helper first.
- Load data failure in data provider helper, data asset name
- Start index or length is invalid.
AI-assisted analysis of EllanJiang/GameFramework@d0c010b051 (2026-09-15).
Data as JSON: /api/errors/11849514f3d10e65.
Report an issue: GitHub.
Appendix: source
Thrown at GameFramework/Base/DataProvider/DataProvider.cs:128
add
{
m_ReadDataDependencyAssetEventHandler += value;
}
remove
{
m_ReadDataDependencyAssetEventHandler -= value;
}
}
/// <summary>
/// 确保二进制流缓存分配足够大小的内存并缓存。
/// </summary>
/// <param name="ensureSize">要确保二进制流缓存分配内存的大小。</param>
public static void EnsureCachedBytesSize(int ensureSize)
{
if (ensureSize < 0)
{
throw new GameFrameworkException("Ensure size is invalid.");
}
if (s_CachedBytes == null || s_CachedBytes.Length < ensureSize)
{
FreeCachedBytes();
int size = (ensureSize - 1 + BlockSize) / BlockSize * BlockSize;
s_CachedBytes = new byte[size];
}
}
/// <summary>
/// 释放缓存的二进制流。
/// </summary>
public static void FreeCachedBytes()
{
s_CachedBytes = null;
}
View on GitHub (pinned to d0c010b051)