Physics.OverlapSphereNonAlloc throws System.AcessViolationException

yo peoples.

Below a very basic script, attached to a game object in a scene with number of box colliders. Run in the editor the script works correctly, drawing a debug line to the detected colliders, and outputing the number of collisions detected to the console.

When I deploy the same scene to the Hololens, I get an exception.

“Attempted to read or write protected memory. This is often an indication that other memory is corrupt.”
StackTrace " at UnityEngineProxy.InternalCalls.PInvokeCalls.Physics_CUSTOM_INTERNAL_CALL_OverlapSphereNonAlloc(IntPtr param_0, Single param_1, Int64 param_2, Int32 param_3, Int32 param_4, Int64& outExceptionHandle)\r\n at UnityEngineProxy.InternalCalls.Physics_CUSTOM_INTERNAL_CALL_OverlapSphereNonAlloc(IntPtr position, Single radius, Object results, Int32 layerMask, Int32 queryTriggerInteraction)\r\n at UnityEngine.Physics.INTERNAL_CALL_OverlapSphereNonAlloc(Vector3& position, Single radius, Collider[ ] results, Int32 layerMask, QueryTriggerInteraction queryTriggerInteraction)\r\n at UnityEngine.Physics.OverlapSphereNonAlloc(Vector3 position, Single radius, Collider[ ] results)\r\n at Test.Update()\r\n at Test.$Invoke6Update(Int64 instance, Int64* args)\r\n at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)" string

Any thoughts, suggestions etc would be much appreciated.

cheers
Vinnie

public class Test : MonoBehaviour {

    private Collider[] overlapResults = new Collider[10];

    public int NumberOfCollisions = 0;


    // Use this for initialization
    void Start () {
      
    }
  
    // Update is called once per frame
    void Update () {
        NumberOfCollisions = Physics.OverlapSphereNonAlloc(transform.position, 10f, overlapResults);

        for (int i = 0; i < NumberOfCollisions; i++)
        {
            Debug.DrawLine(transform.position, overlapResults[i].transform.position, Color.red);
        }

        Debug.Log("collisions " + this.NumberOfCollisions);
    }
}

Hello,

The API call Debug.DrawLine doesn’t work on the HoloLens because it is a gizmo, these don’t render on the HoloLens. My suggestion is to do a Physics.Raycast from the HoloLens camera, then draw a line using the line renderer from the HoloLens camera to the Ray Cast Hit point.

Let me know if you have questions.

That bug of it causing access violation was fixed in 5.6.1:

https://unity3d.com/unity/whats-new/unity-5.6.1

1 Like

Thanks for that, the Exception is thrown before the Debug gizmo code on Line 15.

Cheers

Awesome, thank you will give that a try

I can confirm 5.6.1 fixes this error.