Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ruby FFI tries to be symmetric about who owns memory - if you allocate it (i.e. the C code), you have to free it. Conversely, if FFI allocates it, it alone can free it.</p> <p>You didn't post your FreeStrings() function, but assuming it looks a bit like:</p> <pre><code>void FreeStringArray(char **strings, int len) { for (int i = 0; i &lt; len; ++i) { delete[] strings[i]; } // Do _NOT_ free 'strings' itself, that is managed by FFI } </code></pre> <p>And you use it thusly:</p> <pre><code>module SomeDll extend FFI::Library ffi_lib 'SomeDll.dll' attach_function :get_strings, :ReturnAnArrayOfStrings, [:pointer], :int attach_function :free_strings, :FreeStringArray, [ :pointer, :int ], :void end include SomeDll count = get_strings(nil) strings = FFI::MemoryPointer.new :pointer, count get_strings strings strings.get_array_of_string(0, count).each do |value| puts value end # free each element of the array free_strings(strings, count) </code></pre> <p>Then that should work. </p> <p>The equivalent C code would be:</p> <pre><code>int count = ReturnArrayOfStrings(NULL); // Allocate an array for the pointers. i.e. FFI::MemoryPointer.new :pointer, count char **ptr_array = (char **) calloc(count, sizeof(char *)); ReturnArrayOfStrings(ptr_array); for (int i = 0; i &lt; count; ++i) { printf("string[%d]=%s\n", i, ptr_array[i]); } // Free each element of the array (but not the array itself) FreeStringArray(ptr_array, count); // free the array itself. i.e FFI::MemoryPointer garbage-collecting its memory free(ptr_array); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload