Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Delegate functions cause "Access violation" from Hololens DLL into C# after 15-60 seconds

Discussion in 'VR' started by blainebell, Jul 18, 2017.

  1. blainebell

    blainebell

    Joined:
    Jun 10, 2016
    Posts:
    28
    I have implemented a DLL used in my Unity Plugin that has been converted to the Universal Windows Platform and it seems to work on both the Hololens Emulator and device. After a certain period of time, I get the error:

    "Access violation writing location 0x00000005"

    The stack trace is at the c++ call of the delegate function. I took out everything in the Update() functions except a c++ call to callback this delegate, so there is *no* memory allocation/deallocation throughout the duration of the program (except for initialization). Hence, this should not be a memory leak (confirmed by the diagnostic tools on the emulator, no new instances). This delegate pointer is being consistently used, and all of a sudden it becomes corrupt.

    I have been second-guessing how this DLL has been built, and would appreciate any guidance on whether anyone knows something that could be causing this.

    I am using Visual Studio 2017 to build this Universal Windows Platform DLL, I have tried both 2015 and 2017 "Platform Toolset", but I am now using 2015 to be safe (I see that a default/empty project exports to have a reference to 2015 runtime). I am also linking the DLL to WindowsApp.lib and using "Console" SubSystem. (could these cause problems?)

    There are of course a million other possible parameters to building a DLL, but if anyone has had experience with using DLLs with the Hololens, please let me know how to fix this.

    Thanks!

    Blaine
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,720
    Are you by any chance not keeping the reference to delegate which you pass to native side? It's getting garbage collected if you're not.
     
  3. blainebell

    blainebell

    Joined:
    Jun 10, 2016
    Posts:
    28
    Well well, that sounds promising! Not sure why this needs to be done with Hololens, but not for any other platform. But in any case, I will try this, much appreciated!!!
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,720
    This needs to be done on all platforms. You may have been getting lucky.
     
  5. blainebell

    blainebell

    Joined:
    Jun 10, 2016
    Posts:
    28
    That worked. So this could be related, but I was having an issue with allocating global static memory on the heap inside the DLL. This shouldn't be a problem, should it? In my case, when the DLL was loaded, the garbage collector could have been cleaning up the Delegates, since no references were kept.

    Thanks a lot for your responses!

    Blaine
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,720
    That doesn't sound related. When you say issues allocating global static memory, do you mean crashing or it returning NULL? Or something else entirely? How are you allocating memory?
     
  7. blainebell

    blainebell

    Joined:
    Jun 10, 2016
    Posts:
    28
    Allocating memory is instantiating a class instance globally, where the constructor might be using new or malloc to allocate member variables. You can also allocate memory with global variables by using brace initialization, which typically will allocate at least some memory in the heap.

    I think it could be related, and will go back and test my simple projects for sure.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,720
    Global variables are not stored on the heap, unless specifically placed there with malloc, new or HeapAlloc.

    But how is it crashing?
     
  9. blainebell

    blainebell

    Joined:
    Jun 10, 2016
    Posts:
    28
    I went back to my simple examples and it is crashing in the same way, when the delegate function gets called. I think it is not because of the global heap memory, but if there is memory allocated *after* the delegates get instantiated. Since keeping references to the delegates fixes this issue, it can be closed. Thanks again for your quick response, I really appreciate it!

    Blaine