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

2229538--148527--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?

    void Update()
    {
        // Allocate memory for two 3D positions
        byte[] buffer = new byte[24];
        GCHandle bufHdle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
       
        // dll function fills byte array with data
        fooDLLfunction(bufHdle.AddrOfPinnedObject());

        Vector3[] positions = new Vector3[2];
        for (int i = 0; i < 2; i++)
        {
            // Get index of position "i" in byte array
            IntPtr ptr = new IntPtr(bufHdle.AddrOfPinnedObject().ToInt64() + Convert.ToInt64(Marshal.SizeOf(typeof(Vector3)) * i));
            positions[i] = (Vector3)Marshal.PtrToStructure(ptr, typeof(Vector3));
        }

        bufHdle.Free();
    }

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!

Put a space between the : and the P. Like so : P. Put them together and it makes a smiley face. Like this :stuck_out_tongue:

I’m afraid I can’t actually help with the memory issue.

1 Like

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.

4341067--392017--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?

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.