# Segmentation fault when calling fortran function

**URL:** <https://discourse.julialang.org/t/segmentation-fault-when-calling-fortran-function/53319>\
**Category:** New to Julia\
**Tags:** fortran\
**Created:** [January 14, 2021, 3:28am UTC](https://discourse.julialang.org/t/segmentation-fault-when-calling-fortran-function/53319 "2021-01-14T03:28:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![k945](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k945/32/8906_2.png) [@k945](https://discourse.julialang.org/u/k945)\
**Post date:** [January 14, 2021, 3:28am UTC](https://discourse.julialang.org/t/segmentation-fault-when-calling-fortran-function/53319/1 "2021-01-14T03:28:22Z")

</div>

I am a bit lost with a script a fortran script that I am trying to use. I run into a seg fault error when calling dcapecalc2d from [rip\_cape.f90](([https://github.com/NCAR/wrf-python/blob/develop/fortran/rip\_cape.f90](https://github.com/NCAR/wrf-python/blob/develop/fortran/rip_cape.f90)).

```julia
signal (11): Segmentation fault
in expression starting at /data/kwamahiu/codepen/test.jl:45
fstrlen at /src/gcc-7.3.0/libgfortran/runtime/string.c:37 [inlined]
fc_strdup at /src/gcc-7.3.0/libgfortran/runtime/string.c:129
find_file at /src/gcc-7.3.0/libgfortran/io/unix.c:1730
new_unit at /src/gcc-7.3.0/libgfortran/io/open.c:518
already_open at /src/gcc-7.3.0/libgfortran/io/open.c:704 [inlined]
st_open at /src/gcc-7.3.0/libgfortran/io/open.c:883
dlookup_table_ at /data/kwamahiu/codepen/rip_cape.so (unknown line)
dcapecalc2d_ at /data/kwamahiu/codepen/rip_cape.so (unknown line)
top-level scope at /data/kwamahiu/codepen/test.jl:45
jl_toplevel_eval_flex at /buildworker/worker/package_linux64/build/src/toplevel.c:834
jl_parse_eval_all at /buildworker/worker/package_linux64/build/src/ast.c:913
jl_load_rewrite at /buildworker/worker/package_linux64/build/src/toplevel.c:914
include at ./Base.jl:380
include at ./Base.jl:368
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2214 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2398
exec_options at ./client.jl:296
_start at ./client.jl:506
jfptr__start_53898.clone_1 at /data/kwamahiu/julialang/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2214 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2398
unknown function (ip: 0x401931) 
unknown function (ip: 0x401533)
__libc_start_main at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
unknown function (ip: 0x4015d4)
Allocations: 18403097 (Pool: 18399264; Big: 3833); GC: 20
Segmentation fault (core dumped)

```

In case they are needed, these are the required files: [rip\_cape90](https://github.com/NCAR/wrf-python/blob/develop/fortran/rip_cape.f90), [wrf\_constants.f90](https://github.com/NCAR/wrf-python/blob/develop/fortran/wrf_constants.f90) and [psadilookup.dat](https://github.com/NCAR/wrf-python/blob/develop/src/wrf/data/psadilookup.dat)

I downloaded the files and compile them thusly

```julia
gfortran -c wrf_constants.f90
gfortran rip_cape.f90 -o rip_cape.so -shared -fPIC

```

and this is the script that I am using to call the relevant function

```julia
full_p = rand(19.8566:1.0:1026.856, 120,97,8)
t = rand(188.094:0.23:305.0942, 120,97,8)
full_ph = rand(10.50011:0.37:27877.50, 120, 97, 8)
qvapor = rand(0.0:0.01:0.021, 120,97,8)
psfc = rand(754.52:1031.38, 120,97,8)
ter = rand(-10:2512.17, 120,97,8)

mix, mjy, mkzh = size(full_p)
cape, cin = zeros(Float64, mix, mjy, mkzh)
prsf, prs_new = tmk_new = qvp_new = ght_new = zeros(Float64, mkzh, mix, mjy)

ter_follow = 1
i3dflag = 0
errmsg = Vector{Cstring}(undef, 512)
errstat = 0
cmsg= 9.969209968386869

ccall((:dcapecalc2d_,
        "rip_cape.so"),
        Cvoid,
        (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Integer}, Ref{Integer}, Ref{Integer}, Ref{Integer},
         Ptr{UInt8}, Ref{Integer}, Ptr{UInt8}),
        full_p, t, qvapor, full_ph, ter, psfc,
        cape, cin, prsf, prs_new, tmk_new, qvp_new, ght_new,
        cmsg, mix, mjy, mkzh, ter_follow,
        "psadilookup.dat", errstat, errmsg)

```

Can anybody tell me what I have done wrong and how to fix this.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 14, 2021, 5:06am UTC](https://discourse.julialang.org/t/segmentation-fault-when-calling-fortran-function/53319/2 "2021-01-14T05:06:43Z")

</div>

Ref{Integer} seems unlikely. Does Ref{Int} work there?

---

<div class="post-metadata">

**Author:** ![k945](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k945/32/8906_2.png) [@k945](https://discourse.julialang.org/u/k945)\
**Post date:** [January 14, 2021, 12:33pm UTC](https://discourse.julialang.org/t/segmentation-fault-when-calling-fortran-function/53319/3 "2021-01-14T12:33:00Z")

</div>

Thanks at @jameson. Chaning `Ref{Integer}` to `Ref{Int}` has gotten rid of the seg fault.

However, I now get the following

```julia
At line 149 of file rip_cape.f90
Fortran runtime error: End of record

```

I think this is because the fortran script is unable to read the file. I have had a look at the [Bit Types](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#man-bits-types) section in the documentation using Ptr{UInt8} for strings. Is this correct?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [January 14, 2021, 11:16pm UTC](https://discourse.julialang.org/t/segmentation-fault-when-calling-fortran-function/53319/4 "2021-01-14T23:16:20Z")

</div>

In gfortran, strings are passed as two arguments, a Ptr{UInt8}, and a _trailing_ long (often 32-bits)

---

<div class="post-metadata">

**Author:** ![k945](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k945/32/8906_2.png) [@k945](https://discourse.julialang.org/u/k945)\
**Post date:** [January 15, 2021, 6:00am UTC](https://discourse.julialang.org/t/segmentation-fault-when-calling-fortran-function/53319/5 "2021-01-15T06:00:44Z")

</div>

@jameson I made add the trailing long bits as per your suggestion

```julia
ccall((:dcapecalc3d_,
        "/data/kwamahiu/codepen/rip_cape.so"),
        Cvoid,
        (Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64},
         Ref{Float64}, Ref{Int}, Ref{Int}, Ref{Int}, Ref{Int},
         Ptr{UInt8}, Ref{Int}, Ptr{UInt8}, UInt32, UInt32),    
                                            ^^^^^^^^^^^^
        full_p, t, qvapor, full_ph, ter, psfc,
        cape, cin, prsf, prs_new, tmk_new, qvp_new, ght_new,
        cmsg, mix, mjy, mkzh, ter_follow,
        fname, errstat, errmsg, sizeof(fname), sizeof(errmsg))
                                       ^^^^^^^^^^^^

```

Everything seems to be working.

Thanks.
