{"record":{"id":"b8ae358a45708b65","repo":"stride3d/stride","slug":"tried-to-unregister-soundeffectinstance-while-not-contained","errorCode":null,"errorMessage":"Tried to unregister soundEffectInstance while not contained in the instance list.","messagePattern":"Tried to unregister soundEffectInstance while not contained in the instance list\\.","errorType":"exception","errorClass":"AudioSystemInternalException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Audio/SoundBase.cs","lineNumber":125,"sourceCode":"        /// </summary>\n        /// <param name=\"mainInstance\">The main instance of the sound effect</param>\n        internal void StopConcurrentInstances(SoundInstance mainInstance)\n        {\n            foreach (var instance in Instances)\n            {\n                if (instance != mainInstance)\n                    instance.Stop();\n            }\n        }\n\n        /// <summary>\n        /// Unregister a disposed Instance.\n        /// </summary>\n        /// <param name=\"instance\"></param>\n        internal void UnregisterInstance(SoundInstance instance)\n        {\n            if (!Instances.Remove(instance))\n                throw new AudioSystemInternalException(\"Tried to unregister soundEffectInstance while not contained in the instance list.\");\n        }\n\n        /// <summary>\n        /// Register a new instance to the soundEffect.\n        /// </summary>\n        /// <param name=\"instance\">new instance to register.</param>\n        protected void RegisterInstance(SoundInstance instance)\n        {\n            Instances.Add(instance);\n            intancesCreationCount++;\n        }\n\n        /// <summary>\n        /// The number of Instances Created so far by this SoundEffect. Used only to give a unique name to the SoundEffectInstance.\n        /// </summary>\n        protected int intancesCreationCount;\n\n        /// <summary>","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Audio/SoundBase.cs#L107-L143","documentation":"SoundBase.UnregisterInstance throws AudioSystemInternalException when the given SoundInstance is not present in the sound's Instances list, i.e. disposal code is trying to unregister an instance that was never registered or already removed. This signals a broken internal bookkeeping invariant rather than a caller error.","triggerScenarios":"Disposing a SoundInstance whose parent sound never registered it, double-disposing an instance, or audio-system teardown removing an instance twice (e.g. Dispose called from two paths).","commonSituations":"Race conditions between audio-device teardown and instance disposal; manually calling Dispose twice; mixing instances across AudioSystems so disposal hits the wrong registry.","solutions":["Ensure every instance is registered to its parent sound exactly once and disposed exactly once (guard with IsDisposed).","Don't share SoundInstance objects across different AudioEngine/AudioSystem instances.","If reproducible in normal use, report as a Stride bug with a reproduction; this exception indicates an engine invariant break.","Wrap teardown/Dispose logic so double-disposal cannot occur (idempotent dispose)."],"exampleFix":"// before\ninstance.Dispose();\ninstance.Dispose(); // second call triggers unregister failure\n// after\nif (!instance.IsDisposed)\n{\n    instance.Dispose();\n}","handlingStrategy":"try-catch","validationCode":"// Caller-side guard: dispose each instance at most once\nbool disposedAlready = instance.IsDisposed;","typeGuard":null,"tryCatchPattern":"try\n{\n    instance.Dispose();\n}\ncatch (AudioSystemInternalException ex) when (ex.Message.Contains(\"unregister soundEffectInstance\"))\n{\n    logger.Warn(\"Instance was not registered or already removed; possible double-dispose.\");\n}","preventionTips":["Make dispose paths idempotent and check IsDisposed before disposing.","Never dispose the same SoundInstance from two code paths (e.g. device reset + explicit cleanup).","Keep instances tied to a single AudioSystem; don't migrate them between engines.","If reproducible outside teardown races, file a Stride bug — this exception marks broken engine invariants."],"tags":["audio","internal","lifecycle","dispose","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}