c# - Can I copy references to unmanaged memory without causing memory leaks? -


context

i'm using emgucv wrapper around opencv. can imagine, emgu full of classes intptr pointers unmanaged memory, this property of iimage interface, implemented mat, umat, , image<,>.

i don't know if related, inherit emgu's own disposableobject , unmanagedobject abstract classes.

  • i'm comfortable working in java, python, , ruby garbage collectors pretty smart.
  • i'm comfortable working in c, heap arrays , structs own problem.

in java-calling-c flavored jni native interop i've done, arrays explicitly extracted , converted jvm heap using jni functions. can mess array in c, have use jni functions again either copy array java returnable, or output parameter. isolates c memory management , java memory management - aren't concerned each other.

but in c# native interop world, see can use intptr pointers directly! that's convenient if want clever/dangerous fast dereferencing , pointer arithmetic, i'm pretty squeamish manipulating them or storing references them.

question

if this

var img = new image<rgb, byte>("filename.jpg"); intptr myptr = img.ptr; 

does mess garbage collector unmanaged memory , cause memory leaks? or myptr link in reference graph keeping img being automatically disposed?

or depend on native code , wrapper? (open/emgucv in case).


Comments