Replies: 2 comments 1 reply
-
Julia provides a built in solution to this problem that can be acessed via Julia.GCPUSH & Julia.GCPOP. When you push the object using this method, it will preserve its references until it is popped. However this requires the following structure of code: Julia.GCPUSH(obj); It is kind of bulky to use which is why I provided an easier interface: Say we have have a C# int we want to box. You would do var i = new JLVAL(5); The GC has no idea we want to keep it alive. In order to keep it alive, do the following: var handle = i.Pin(); The handle has a destructor that will auto free itself when it goes out of scope or you can free it earlier. I can also make an object stay pinned even if the handle goes out of the scope but that is unsafe and I dont have an interface for that at the moment. It is just a possibility. My next update reworks this feature to be more efficient |
Beta Was this translation helpful? Give feedback.
-
Sorry, I did not notice the |
Beta Was this translation helpful? Give feedback.
-
For example, the following constructor allocates a boxed Float64 on the Julia side, but we need to keep it alive:
https://github.com/HyperSphereStudio/JULIA.Net/blob/3bd7bf222bea19d7e82b899888a4237ef16e1c75/src/csharp/JLVal.cs#L23
In the memory management documentation, there is
My understanding is that, even if you are still using the object in C#, Julia is unaware of it. Consequently, Julia may deallocate it without your control and the
IntPtr
stored inside theJLVal
instance becomes invalid.Of course, all other data objects like a Julia array need such proper memory management.
Beta Was this translation helpful? Give feedback.
All reactions