Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice

(Case 1343011) No longer possible to use GCHandle in jobs

Discussion in '2021.2 Beta' started by halfvoxel, Jun 13, 2021.

  1. halfvoxel

    halfvoxel

    Joined:
    Nov 4, 2013
    Posts:
    16
    Previously I have several times used GCHandle (https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.gchandle?view=net-5.0) to move data into jobs that can for various reasons not be converted to pure structs.
    However, in Unity 2021.2a this produces an error:

    System.InvalidOperationException: ManagedJob.handle.handle uses unsafe Pointers which is not allowed. Unsafe Pointers can lead to crashes and no safety against race conditions can be provided.
    If you really need to use unsafe pointers, you can disable this check using [NativeDisableUnsafePtrRestriction].

    Unfortunately this error points to an internal field of the GCHandle struct and it's not possible to add the NativeDisableUnsafePtrRestriction attribute to that field since its internal to the C# stdlib. The error doesn't go away if I add the NativeDisableUnsafePtrRestriction attribute to the GCHandle field.

    Was this an intentional change to prevent all usages of GCHandles in Unity job code? That would be very bad for me since I have a lot of code that simply cannot work without it. Unity's UnsafeUtility.PinGCObjectAndGetAddress is not sufficient, as I don't think that can be used with managed objects. At least I have not found any way to use it with managed objects. The most obvious candidate UnsafeUtility.AsRef only works with non-nullable types.
     
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Did you try adding the NativeDisableUnsafePtrRestriction attribute to your job struct definition?
     
  3. halfvoxel

    halfvoxel

    Joined:
    Nov 4, 2013
    Posts:
    16
    The `NativeDisableUnsafePtrRestriction` attribute can only be added to fields, and adding that attribute to the field holding the GCHandle does nothing. The error refers to the internal field inside the GCHandle, but I cannot add an attribute to that field.
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    That's terrible. Thanks Unity, for killing a completely valid use case for advanced devs!

    I would fire off bug reports ASAP. Unity staff has a very spotty record for replying to forum threads.
     
  5. halfvoxel

    halfvoxel

    Joined:
    Nov 4, 2013
    Posts:
    16
    Last edited by a moderator: Jun 14, 2021
    laurentlavigne likes this.
  6. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    I dont fully understand how you use GCHandle in jobs but as I know you can convert in into Int64 pass to job and do what ever you want with it, including covert back to GCHandle struct:

    Code (CSharp):
    1.        GCHandle h        = get handle logic;
    2.         IntPtr p        = (IntPtr)h;
    3.         var intHandle    =  p.ToInt64();
    4.        
    5.         //pass intHandle to job
    6.        
    7.         //and in job
    8.        
    9.         var handle = GCHandle.FromIntPtr( (IntPtr)intHandle );
     
    Neto_Kokku likes this.
  7. halfvoxel

    halfvoxel

    Joined:
    Nov 4, 2013
    Posts:
    16
    That's a good point @Jes28. Thanks!
    Could probably be used as a workaround. It would still be nice not to have to add all that boilerplate though.
     
  8. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
  9. halfvoxel

    halfvoxel

    Joined:
    Nov 4, 2013
    Posts:
    16
    LeonhardP likes this.
  10. Alex-Thibodeau

    Alex-Thibodeau

    Unity Technologies

    Joined:
    Apr 18, 2013
    Posts:
    27
    @halfvoxel thanks for the bug report! This is not intended behavior and I am working on resolving it.
     
    LeonhardP likes this.
  11. caleidon

    caleidon

    Joined:
    Oct 21, 2020
    Posts:
    5
    Any updates on this?
     
  12. CGBull

    CGBull

    Joined:
    Feb 12, 2017
    Posts:
    82
    Any updates for fix this?
     
  13. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135