Possible cause of IndexOutOfRangeException thrown by ApplicationData.Current?.LocalSettings?

Hong 1,211 Reputation points
2023-09-05T14:45:40.4633333+00:00

ApplicationDataContainer localSettings = ApplicationData.Current?.LocalSettings;

The above code occasionally throws the following exception:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
 Stack Trace:
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowIndexOutOfRangeException() + 0x25
   at System.Collections.Generic.Internal.Dictionary`2.Insert(TKey, TValue, Boolean, Int32) + 0x1cc
   at System.Collections.Generic.Internal.Dictionary`2.Add(TKey, TValue, Int32) + 0x12
   at System.Runtime.InteropServices.ComObjectCache.Add(IntPtr, __ComObject) + 0xcc
   at System.__ComObject.__Attach(IntPtr, RuntimeTypeHandle) + 0xfe
   at System.__ComObject.__AttachingCtor(IntPtr, RuntimeTypeHandle) + 0x4f
   at System.__ComObject.AttachingCtor(__ComObject, IntPtr, RuntimeTypeHandle) + 0x34
   at System.Runtime.InteropServices.CalliIntrinsics.Call[T](IntPtr, __ComObject, IntPtr, RuntimeTypeHandle) + 0x30
   at SharedLibrary!<BaseAddress>+0x864140
   at SharedLibrary!<BaseAddress>+0x863f72
   at SharedLibrary!<BaseAddress>+0x860875
   at SharedLibrary!<BaseAddress>+0x8607f4
   at SharedLibrary!<BaseAddress>+0x860696
   at System.Runtime.InteropServices.McgMarshal.ComInterfaceToObjectInternal(IntPtr, RuntimeTypeHandle, RuntimeTypeHandle) + 0x10
   at System.Runtime.InteropServices.McgMarshal.ComInterfaceToObject(IntPtr, RuntimeTypeHandle, RuntimeTypeHandle) + 0xbc
   at System.Runtime.InteropServices.McgMarshal.ComInterfaceToObject(IntPtr, RuntimeTypeHandle) + 0xb
   at __Interop.ForwardComStubs.Stub_3[TThis, TResult](__ComObject, Int32) + 0x4e
   at Windows.Storage.ApplicationData.get_LocalSettings() + 0x18


Could anyone offer a tip on the possible cause of this? Let me emphasize again this happens only occasionally. It is not a big deal. The exception can be caught and it is not catastrophic.

I wanted to add, on the same PC, IndexOutOfRangeException is also thrown by the following at two different locations:

MediaStreamSample mss = MediaStreamSample.CreateFromBuffer(sd.getData().AsBuffer(), ts);

They seem to be related. I wonder if there is a general condition that causes IndexOutOfRangeException in many places.

Universal Windows Platform (UWP)
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.