Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions ext/rubydex/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ static VALUE rdxr_graph_index_all(VALUE self, VALUE file_paths) {
size_t error_count = 0;
const char *const *errors = rdx_index_all(graph, (const char **)converted_file_paths, length, &error_count);

// Free the converted file paths and allow the GC to collect them
for (size_t i = 0; i < length; i++) {
free(converted_file_paths[i]);
}
free(converted_file_paths);
rdxi_free_str_array(converted_file_paths, length);

if (errors == NULL) {
return rb_ary_new();
Expand Down Expand Up @@ -348,10 +344,7 @@ static VALUE rdxr_graph_resolve_constant(VALUE self, VALUE const_name, VALUE nes
const CDeclaration *decl =
rdx_graph_resolve_constant(graph, StringValueCStr(const_name), (const char **)converted_file_paths, length);

for (size_t i = 0; i < length; i++) {
free(converted_file_paths[i]);
}
free(converted_file_paths);
rdxi_free_str_array(converted_file_paths, length);

if (decl == NULL) {
return Qnil;
Expand Down Expand Up @@ -379,10 +372,7 @@ static VALUE rdxr_graph_resolve_require_path(VALUE self, VALUE require_path, VAL

const uint64_t *uri_id = rdx_resolve_require_path(graph, path_str, (const char **)converted_paths, paths_len);

for (size_t i = 0; i < paths_len; i++) {
free(converted_paths[i]);
}
free(converted_paths);
rdxi_free_str_array(converted_paths, paths_len);

if (uri_id == NULL) {
return Qnil;
Expand All @@ -407,10 +397,7 @@ static VALUE rdxr_graph_require_paths(VALUE self, VALUE load_path) {
size_t out_count = 0;
const char *const *results = rdx_require_paths(graph, (const char **)converted_paths, paths_len, &out_count);

for (size_t i = 0; i < paths_len; i++) {
free(converted_paths[i]);
}
free(converted_paths);
rdxi_free_str_array(converted_paths, paths_len);

if (results == NULL) {
return rb_ary_new();
Expand Down
10 changes: 10 additions & 0 deletions ext/rubydex/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ char **rdxi_str_array_to_char(VALUE array, size_t length) {
return converted_array;
}

// Free a char** array allocated by rdxi_str_array_to_char
void rdxi_free_str_array(char **array, size_t length) {
if (array != NULL) {
for (size_t i = 0; i < length; i++) {
free(array[i]);
}
free(array);
}
}

// Verify that the Ruby object is an array of strings or raise `TypeError`
void rdxi_check_array_of_strings(VALUE array) {
Check_Type(array, T_ARRAY);
Expand Down
5 changes: 4 additions & 1 deletion ext/rubydex/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#include "ruby.h"

// Convert a Ruby array of strings into a double char pointer so that we can pass that to Rust.
// This copies the data so it must be freed
// This copies the data so it must be freed with rdxi_free_str_array
char **rdxi_str_array_to_char(VALUE array, size_t length);

// Free a char** array allocated by rdxi_str_array_to_char
void rdxi_free_str_array(char **array, size_t length);

// Verify that the Ruby object is an array of strings or raise `TypeError`
void rdxi_check_array_of_strings(VALUE array);

Expand Down
Loading