CxxWrap.jl icon indicating copy to clipboard operation
CxxWrap.jl copied to clipboard

Calling `wrapmodule` in same module `include(depsfile)` triggers segfault

Open SylvainCorlay opened this issue 5 years ago • 3 comments

upon call of any wrapped in wrapmodule.

I am reproducing this in the xtensor-julia-cookiecutter project here: https://github.com/QuantStack/xtensor-julia-cookiecutter/pull/9

SylvainCorlay avatar Oct 31 '18 07:10 SylvainCorlay

Ran into the same issue. Resolved by including the __init__() function from the README.

# Load the module and generate the functions
module CppHello
  using CxxWrap
  @wrapmodule(joinpath("path/to/built/lib","libhello"))

  function __init__()
    @initcxx
  end
end

# Call greet and show the result
@show CppHello.greet()

JoshChristie avatar Nov 05 '18 01:11 JoshChristie

Yes, that's exactly it, thanks for replying Josh. To expand a bit on this, __init__ is now required because precompilation is on by default in Julia 1.0. Function pointers are stored in an array that is created by @wrapmodule and filled with up-to-date values by @initcxx. If you turn off precompilation the version with just @wrapmodule would also work, but that's not recommended of course.

barche avatar Nov 05 '18 06:11 barche

I have run into something similar in relation to a CxxWrap-based-submodule, say CppHello in a project, say Foo.jl:

module Foo
  module CppHello
    using CxxWrap
    @wrapmodule(joinpath("path/to/built/lib","libhello"))

    function __init__()
      @initcxx
    end
  end

  greet() = CppHello.greet()

  export greet
end

A sample like this causes a segmentation fault:

using Foo
greet()

Strangely, this seemed to work

include("src/Foo.jl")
using .Foo
greet()

Disabling precompilation seems to work as well.

Actual code is here: https://github.com/IHPSystems/pylon_julia_wrapper/blob/master/src/wrapper.jl

stemann avatar Mar 27 '19 09:03 stemann