Embedding Julia in C++, segmentation fault during GC

I need to call a Julia function from C++ and get segmentation fault when Julia performs GC during the function. I don’t such an error if I manually disable GC for Julia. I have reduced it to a simple case where the problem can be reproduced.

Julia version: 0.5.1

The Julia function that I need to call

function map_values_func(values::Vector)::Tuple{Vector{Int64}, Vector{Float32}}
    @time map_values_func_helper(values)
end

function map_values_func_helper(values::Vector)::Tuple{Vector{Int64}, Vector{Float32}}
    output_keys = Vector{Int64}()
    output_values = Vector{Float32}()
    println("length = ", length(values))
    for i = 1:length(values)
        if i % 10000 == 0
            println(i)
        end
        push!(output_values, values[i] / 10)
    end
    println("num_of_output_values = ", length(output_values))
    return (output_keys, output_values)
end

The C++ program that calls this function

#include <julia.h>
#include <glog/logging.h>
#include <vector>
#include <iostream>

const size_t kNumElements = 800000 * 10;

jl_function_t* GetFunction(jl_module_t* module,
            const char* func_name) {
  auto* func = jl_get_function(module, func_name);
  if (func == nullptr) {
    std::cout << "cannot find function " << func_name << std::endl;
    abort();
  }
  return func;
}


int main(int argc, char *argv[]) {
  std::cout << "hello world!" << std::endl;
  jl_init(NULL);
  jl_load("/home/ubuntu/test_julia/run_map.jl");
  //jl_gc_enable(0);
  std::vector<float> values(kNumElements, 0.1);

  jl_value_t *input_array_jl,
      *output_tuple_jl,
      *output_array_jl;

  JL_GC_PUSH3(&input_array_jl,
              &output_tuple_jl,
              &output_array_jl);
  jl_value_t *input_array_type = jl_apply_array_type(jl_float32_type, 1);
  input_array_jl = reinterpret_cast<jl_value_t*>(
      jl_ptr_to_array_1d(input_array_type, values.data(), kNumElements, 0));

  jl_function_t *mapper_func = GetFunction(jl_main_module, "map_values_func");
  output_tuple_jl = jl_call1(mapper_func, input_array_jl);
  output_array_jl = jl_get_nth_field(output_tuple_jl, 1);

  uint8_t *output_array = reinterpret_cast<uint8_t*>(jl_array_data(output_array_jl));
  float* output_values = reinterpret_cast<float*>(output_array);
  JL_GC_POP();
  jl_atexit_hook(0);
  std::cout << "values[10] = " << output_values[10] << std::endl;
}

Here is the error message:

signal (11): Segmentation fault
while loading no file, in expression starting on line 0
push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1367
gc_push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1183 [inlined]
gc_mark_stack at /home/ubuntu/julia-0.5.1/src/gc.c:1262
push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1454
gc_push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1183 [inlined]
gc_mark_module at /home/ubuntu/julia-0.5.1/src/gc.c:1212
push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1447
gc_push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1183 [inlined]
gc_mark_module at /home/ubuntu/julia-0.5.1/src/gc.c:1212
push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1447
gc_push_root at /home/ubuntu/julia-0.5.1/src/gc.c:1183 [inlined]
pre_mark at /home/ubuntu/julia-0.5.1/src/gc.c:1533
_jl_gc_collect at /home/ubuntu/julia-0.5.1/src/gc.c:1728 [inlined]
jl_gc_collect at /home/ubuntu/julia-0.5.1/src/gc.c:1883
jl_gc_pool_alloc at /home/ubuntu/julia-0.5.1/src/gc.c:828
jl_gc_alloc_ at /home/ubuntu/julia-0.5.1/src/julia_internal.h:169 [inlined]
jl_box_int64 at /home/ubuntu/julia-0.5.1/src/alloc.c:1255
map_values_func_helper at /home/ubuntu/test_julia/run_map.jl:11
macro expansion at ./util.jl:188 [inlined]
map_values_func at /home/ubuntu/test_julia/run_map.jl:2
unknown function (ip: 0x7f44df3a8772)
jl_call_method_internal at /home/ubuntu/julia-0.5.1/src/julia_internal.h:210 [inlined]
jl_apply_generic at /home/ubuntu/julia-0.5.1/src/gf.c:1950
jl_apply at /home/ubuntu/julia-0.5.1/src/julia.h:1392 [inlined]
jl_call1 at /home/ubuntu/julia-0.5.1/src/jlapi.c:161
unknown function (ip: 0x40105a)
__libc_start_main at /build/glibc-bfm8X4/glibc-2.23/csu/../csu/libc-start.c:291
unknown function (ip: 0x401178)
Allocations: 1365361 (Pool: 1364491; Big: 870); GC: 0
Segmentation fault (core dumped)

Null initialize your local variables

Thanks!