How to use ccall, cconvert, and unsafe_convert in a convenient and memory-safe way?

No that’s not the case.

Well I don’t see how this can help even if foo get passed a copy. It’ll at most make things worse since your x could be a copy too.

Now what does that mean. If what you are saying is that the obj is always either

  1. referenced by a global variable
  2. appears in a GC.@preserve argument
  3. return value of cconvert during a ccall (i.e. when the control is in the C code and have not returned yet).

Then yes, you are right. You would not need to do anything to make the use of obj.handle valid. Otherwise, not, there’s basically no other well defined reference of obj. Just because you have a local variaible has absolutely nothing to do with the lifetime of the object.