Search Unity

CheckDisalowAllocation. Allocating memory when it is not allowed to allocate memory.

Discussion in 'Scripting' started by MikeGameDev, Jul 31, 2015.

  1. MikeGameDev

    MikeGameDev

    Joined:
    Dec 28, 2012
    Posts:
    19
    CheckDisalowAllocation.png
    I'm getting the above error / crash seemingly at random when I exit play mode from the Unity Editor.

    Here are the relevant lines from my log file:

    I'm working heavily with C++ DLLs which I get values from each frame, is the following snippet an acceptable thing to do?

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         // Allocate memory for two 3D positions
    4.         byte[] buffer = new byte[24];
    5.         GCHandle bufHdle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
    6.        
    7.         // dll function fills byte array with data
    8.         fooDLLfunction(bufHdle.AddrOfPinnedObject());
    9.  
    10.         Vector3[] positions = new Vector3[2];
    11.         for (int i = 0; i < 2; i++)
    12.         {
    13.             // Get index of position "i" in byte array
    14.             IntPtr ptr = new IntPtr(bufHdle.AddrOfPinnedObject().ToInt64() + Convert.ToInt64(Marshal.SizeOf(typeof(Vector3)) * i));
    15.             positions[i] = (Vector3)Marshal.PtrToStructure(ptr, typeof(Vector3));
    16.         }
    17.  
    18.         bufHdle.Free();
    19.     }
    The pseudo code above works, I get the expected values, everything works perfectly until I exit play mode which is when the Editor crashes.

    Anyone have any ideas on whats causing this / how I can fix it?

    P.S. How do I remove that damn smiley from my quote!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Put a space between the : and the P. Like so : P. Put them together and it makes a smiley face. Like this :p

    I'm afraid I can't actually help with the memory issue.
     
    MikeGameDev likes this.
  3. MikeGameDev

    MikeGameDev

    Joined:
    Dec 28, 2012
    Posts:
    19
    I seemed to have fixed it by making the byte array and GCHandle members in my class and initializing them once in the Awake function rather than making them each frame.

    Still kinda curious why my first way didn't work but hey, it doesn't crash anymore... for now at least.
     
  4. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,125
    chekmemmory-3.png
    Hello everyone! sorry for resurrecting this thread- I have the exact same message in unity 2018.3.3 when upgrading an old project from unity 5x - I tried 2 times now and it keeps crashing at the same spot, any advice?
     
  5. yuriythebest

    yuriythebest

    Joined:
    Nov 21, 2009
    Posts:
    1,125
    Hey Everybody! I was able to get around this issue by exporting the entire project as a unitypackage ( before upgrading), deleting everything in the project assets, upgrading the "empty" project ( the projectsettings remain), and importing everything "bit by bit" while constantly making backups. The error did reoccur a few times, but this time restarting the project 1-2 times solved these errors.