> I'm trying to create a Ruby extension in Nim,

Interesting, maybe you can collect your insights in a blog or forum post.

I have used Ruby too in the past, and I wrote some C extensions for using CGAL 
and Boost library. While I was told that extending Ruby with C was easier than 
extending Python with C, it was some hard work, mainly due to the CGAL and 
Boost C++ template stuff and due to Ruby GC.

So I am happy that I can do all in Nim now. Well, for Ruby I got the GTK3 
bindings for free, and I still may need CGAL and Boost support...

To your question, returning Nim strings and using that outside of Nim can not 
work out of the box. Nim strings are garbage collected, so when there is no 
longer a reference to a Nim string in the Nim universe, than that string is 
freed by the GC. (I guess for your array example it is similar.). We have 
various possibilities to solve that problem. 1. You can call GC.ref() on the 
returned Nim string, so that CG can not collect it. 2. You can completely turn 
of GC. 3. You may compile your program without GC support. 4. You may copy the 
Nim string to a C string by calling alloc() to create a untraced memory 
location on the heap, copy the string to this location and return that location.

Reply via email to