Ref semantics

I’ve just read the documentation on calling C and Fortran code, and I’m not entirely sure I fully understand what a Ref is, what it does and how it differs from Ptr. Here’s my best guess. I would appreciate if someone with more experience could correct me or confirm its correctness.

A Ptr is just a memory address, and a Ref is a Ptr which additionally tells the garbage collector to take care of the indicated memory. This matches well with the documentation on Ref:

An object that safely references data of type T. This type is guaranteed to point to valid, Julia-allocated memory of the correct type.

It doesn’t match with how Ref is supposed to be used in practice, however. From the calling C code documentation:

Here, the input p is declared to be of type Ref{gsl_permutation}, meaning that the memory that p points to may be managed by Julia or by C.

I assume what is actually happening is that the garbage collector can recognise whether a particular chunk of memory is julia-allocated or not, thus there is no harm in trying to make it track memory for which it is not responsible because it’s simply going to ignore it. On the other hand, there is a lot of harm in not making it track memory properly for which it is responsible as this can cause it to free the memory prematurely. In conclusion, wrapping both C- as well as julia-allocated memory in a Ref amounts to playing it safe.

Finally the documentation also says

If Ptr{Cdouble} were used instead, the ccall() may still work, but Julia’s garbage collector would not be aware that the memory declared for result_array is being used by the external C function. As a result, the code may produce a memory leak if result_array never gets freed by the garbage collector,

How can it happen that gc-controlled memory gets never freed? After all, at some point the memory must become unreachable from within julia and will thus be freed by the garbage collector. This may happen to early if the memory is still used in the C-part of the code, but I don’t see how it could never happen.