I’m experimenting with using CxxWrap to connect an existing C++ library to a Julia project. There’s a particular case I’m looking at right now - there’s a C++ object that has a std::vector as a data member which I want to pass to Julia. Here’s the declaration:
namespace brv {
struct big_random_vector {
std::vector<double> m_vals;
big_random_vector(const double min, const double max);
};
}
Here’s my C++ wrapper code:
#include <vector>
#include <brv.hpp>
#include <jlcxx/jlcxx.hpp>
#include <jlcxx/const_array.hpp>
JLCXX_MODULE define_julia_module(jlcxx::Module& mod) {
mod.add_type<brv::big_random_vector>("big_random_vector")
.constructor<const double, const double>()
.method("get_vals", [](brv::big_random_vector& brv) {
return jlcxx::make_const_array(brv.m_vals.data(), brv.m_vals.size()); });
}
And here’s some Julia code that calls the wrapper:
module BrvWrapper
using CxxWrap
@wrapmodule("libbrv_wrap.so")
function __init__()
@initcxx
end
brv = nothing
function do_everything()
global brv
brv = big_random_vector(-10.0, 10.0)
vals = get_vals(brv)
sqvals = map(x -> x^2, vals)
[]
end
So this works fine. However, if I take out the ‘global brv’ declaration - making brv local - and call do_everything(), julia dumps core:
in expression starting at .../sandbox/juliacpp/cxxwrap_user.jl:34
iterate at ./abstractarray.jl:0 [inlined]
iterate at ./generator.jl:44 [inlined]
collect_to! at ./array.jl:667 [inlined]
collect_to_with_first! at ./array.jl:646 [inlined]
_collect at ./array.jl:640
map at ./array.jl:564 [inlined]
do_everything at .../sandbox/juliacpp/cxxwrap_user.jl:23
##core#424 at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:297
##sample#425 at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:305
sample at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:320 [inlined]
#_lineartrial#41 at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:71
_lineartrial at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:63
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2141 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1631 [inlined]
jl_f__apply at /buildworker/worker/package_linux64/build/src/builtins.c:627
jl_f__apply_latest at /buildworker/worker/package_linux64/build/src/builtins.c:665
#invokelatest#1 at ./essentials.jl:709 [inlined]
invokelatest at ./essentials.jl:708 [inlined]
#lineartrial#38 at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:33 [inlined]
lineartrial at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:33 [inlined]
#tune!#44 at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:135
tune! at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:134 [inlined]
tune! at .../.julia/packages/BenchmarkTools/7aqwe/src/execution.jl:134
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2141 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1631 [inlined]
do_call at /buildworker/worker/package_linux64/build/src/interpreter.c:328
eval_value at /buildworker/worker/package_linux64/build/src/interpreter.c:417
eval_stmt_value at /buildworker/worker/package_linux64/build/src/interpreter.c:368 [inlined]
eval_body at /buildworker/worker/package_linux64/build/src/interpreter.c:778
jl_interpret_toplevel_thunk_callback at /buildworker/worker/package_linux64/build/src/interpreter.c:888
unknown function (ip: 0xfffffffffffffffe)
unknown function (ip: 0x7ff36c954d8f)
unknown function (ip: 0x8)
jl_interpret_toplevel_thunk at /buildworker/worker/package_linux64/build/src/interpreter.c:897
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:814
jl_eval_module_expr at /buildworker/worker/package_linux64/build/src/toplevel.c:181
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:640
jl_parse_eval_all at /buildworker/worker/package_linux64/build/src/ast.c:873
jl_load at /buildworker/worker/package_linux64/build/src/toplevel.c:878
include at ./boot.jl:328 [inlined]
include_relative at ./loading.jl:1105
include at ./Base.jl:31
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2135 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
include at ./client.jl:424
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2135 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1631 [inlined]
do_call at /buildworker/worker/package_linux64/build/src/interpreter.c:328
eval_value at /buildworker/worker/package_linux64/build/src/interpreter.c:417
eval_stmt_value at /buildworker/worker/package_linux64/build/src/interpreter.c:368 [inlined]
eval_body at /buildworker/worker/package_linux64/build/src/interpreter.c:778
jl_interpret_toplevel_thunk_callback at /buildworker/worker/package_linux64/build/src/interpreter.c:888
unknown function (ip: 0xfffffffffffffffe)
unknown function (ip: 0x7ff36bc5e50f)
unknown function (ip: 0xffffffffffffffff)
jl_interpret_toplevel_thunk at /buildworker/worker/package_linux64/build/src/interpreter.c:897
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:814
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:764
jl_toplevel_eval_in at /buildworker/worker/package_linux64/build/src/toplevel.c:843
eval at ./boot.jl:330
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2135 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
eval_user_input at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:86
macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:118 [inlined]
#26 at ./task.jl:333
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2135 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2305
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1631 [inlined]
start_task at /buildworker/worker/package_linux64/build/src/task.c:659
unknown function (ip: 0xffffffffffffffff)
Allocations: 5599103 (Pool: 5598119; Big: 984); GC: 15
[2] 5329 segmentation fault (core dumped) julia
What am I doing wrong?