EllanJiang/GameFramework · error · GameFrameworkException

Capacity is invalid.

Error message

Capacity is invalid.

What it means

An object pool's Capacity is the maximum number of objects it may hold; a negative capacity is nonsensical, so the Capacity setter throws 'Capacity is invalid.' This guards the pool's auto-shrink/eviction math which depends on non-negative capacity (0 means unlimited).

Solutions

  1. Set Capacity to a non-negative integer (0 for unlimited)
  2. Clamp the value before assigning: Math.Max(0, computedCapacity)
  3. Fix the config/source supplying the negative number and validate at load time

Example fix

// before
pool.Capacity = config.PoolCapacity; // may be -1
// after
pool.Capacity = Math.Max(0, config.PoolCapacity);
Defensive patterns

Strategy: validation

Validate before calling

if (capacity < 0) capacity = 0; // 0 = unlimited
pool.Capacity = capacity;

Type guard

bool IsValidCapacity(int c) => c >= 0;

Try / catch

try { pool.Capacity = capacity; }
catch (GameFrameworkException ex) { Log.Error("Invalid pool capacity {0}", capacity); }

Prevention

When it happens

Trigger: Assigning objectPool.Capacity = -1, typically from an unvalidated config value, a subtraction underflow (e.g. capacity - removedCount), or a misparsed settings file.

Common situations: Config-driven pool sizing where the config file has a negative number; computing capacity from another value that becomes negative; UI input allowing negatives.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at GameFramework/ObjectPool/ObjectPoolManager.ObjectPool.cs:131

                {
                    m_AutoReleaseInterval = value;
                }
            }

            /// <summary>
            /// 获取或设置对象池的容量。
            /// </summary>
            public override int Capacity
            {
                get
                {
                    return m_Capacity;
                }
                set
                {
                    if (value < 0)
                    {
                        throw new GameFrameworkException("Capacity is invalid.");
                    }

                    if (m_Capacity == value)
                    {
                        return;
                    }

                    m_Capacity = value;
                    Release();
                }
            }

            /// <summary>
            /// 获取或设置对象池对象过期秒数。
            /// </summary>
            public override float ExpireTime
            {
                get

View on GitHub (pinned to d0c010b051)