Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Dictionary iteration out of sync ( yet no setter inside and no second thread )

Discussion in 'Scripting' started by kiriri, Apr 7, 2017.

  1. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    Hi,
    I'm using Unity 5.6.0xf3 Linux. I have this code :

    Code (CSharp):
    1.         var co = existingChanges.Count;
    2.         var en = existingChanges.GetEnumerator();
    3.         for (int i = 0; i < co; i++)
    4.         {
    5.             en.MoveNext();
    6.             changesA[i] = en.Current.Value;
    7.             changeIndicesA[i] = en.Current.Key;
    8.  
    9.  
    10.         }
    11.         en.Dispose();
    12.         existingChanges.Clear();
    The 'existingChanges' variable is a Dictionary that gets filled and cleared each frame, and can sometimes become really big.
    The problem is, as long as it's not empty, sooner or later this error pops up :
    where InstanceRenderer.cs:85 is en.MoveNext();

    This problem hasn't happened in the past, and I don't know how to fix it. I originally used LINQ on my dictionary and it threw up weird exceptions too.

    My code runs entirely on 1 thread (apart from compute shader calls, which I think work on a separate thread) .

    So as a self-stupidity check, do you think this might be a bug ( I'm suspecting GC ) or am I just missing something glaringly obvious?

    Thanks!
     
    Last edited: Apr 7, 2017
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    The class is named InstanceRenderJob, and looks like it's running in a compute shader? Is this perhaps happening in a multi-threaded context? That would cause an out of sync real fast.
     
  3. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    I was hoping to use multithreading in the long run, but for now I do not, and this part of the code should definitely be single threaded. The exception occurs both in the editor and in standalone, so editor related scripts are out of the question too.

    EDIT:
    The resulting arrays are fed into buffers for compute shaders at a later step, this should be independent from the dictionary though.
     
    Last edited: Apr 7, 2017
  4. kiriri

    kiriri

    Joined:
    Jan 14, 2011
    Posts:
    107
    Solved it, it was indeed sheer stupidity on my part. The GC runs on a separate thread and I had destructors that , in a very convoluted way, accessed the dictionary -_- . 8h of work to find it, because I believed I couldn't have made a mistake *facepalm*

    I solved it via locking the offending functions over the dictionary. Forcing the GC is too expensive.
     
    Last edited: Apr 7, 2017