stride3d/stride · error · ArgumentException

is invalid type

Error message

{ nameof(lockType) } is invalid type

What it means

HeightArrayLock's constructor only handles LockTypes.Read and LockTypes.ReadWrite; any other lockType (e.g. a cast int or removed enum member) reaches the default case and throws ArgumentException "{ lockType } is invalid type".

Solutions

  1. Pass only LockTypes.Read or LockTypes.ReadWrite.
  2. Validate with Enum.IsDefined(typeof(LockTypes), value) before constructing.
  3. Fix numeric serialization of the lock type in calling code.

Example fix

// before
using var l = new HeightArrayLock((LockTypes)mode, lockSlim);
// after
var lt = mode == 0 ? LockTypes.Read : LockTypes.ReadWrite;
using var l = new HeightArrayLock(lt, lockSlim);
Defensive patterns

Strategy: validation

Validate before calling

if (lockType is not (LockTypes.Read or LockTypes.ReadWrite))
    throw new ArgumentException($"Invalid lockType: {lockType}");

Type guard

bool IsValidLockType(LockTypes t) => t is LockTypes.Read or LockTypes.ReadWrite;

Try / catch

try { using var l = new HeightArrayLock(lockType, lockSlim); }
catch (ArgumentException) { /* default to LockTypes.Read */ }

Prevention

When it happens

Trigger: new HeightArrayLock((LockTypes)123, lockSlim) or calling the shape's Lock API with an invalid LockTypes value.

Common situations: LockTypes value built from numeric config; enum drift between library versions.

Understand the failure class

Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/6c616c2670b13e3f. Report an issue: GitHub.

Appendix: source

Thrown at sources/engine/Stride.Physics/Shapes/HeightfieldColliderShape.cs:194

        internal HeightArrayLock(LockTypes lockType, ReaderWriterLockSlim lockSlim)
        {
            if (lockSlim == null)
            {
                throw new ArgumentNullException(nameof(lockSlim));
            }

            readerWriterLockSlim = lockSlim;

            switch (lockType)
            {
                case LockTypes.Read:
                    readerWriterLockSlim.EnterReadLock();
                    break;
                case LockTypes.ReadWrite:
                    readerWriterLockSlim.EnterWriteLock();
                    break;
                default:
                    throw new ArgumentException($"{ nameof(lockType) } is invalid type");
            }
        }

        public void Unlock()
        {
            if (readerWriterLockSlim.IsReadLockHeld)
            {
                readerWriterLockSlim.ExitReadLock();
            }
            else if (readerWriterLockSlim.IsWriteLockHeld)
            {
                readerWriterLockSlim.ExitWriteLock();
            }
        }

        public void Dispose()
        {
            Unlock();

View on GitHub (pinned to 96fad776d2)