Does boxing the variable to System.Object help?

Also, I guess the problem with losing the reference is because when the function ends, the variable is marked for garbage collection.

You could use a 'fixed' block to pin the var in memory, although I'm not sure if this remains after the function is ended.

Additionally you can allocate memory using 'stackalloc' in an unsafe block, which means the GC wont touch it.