# Julia crashes after successful(?) ccall

**URL:** <https://discourse.julialang.org/t/julia-crashes-after-successful-ccall/36157>\
**Category:** General Usage\
**Created:** [March 18, 2020, 6:34pm UTC](https://discourse.julialang.org/t/julia-crashes-after-successful-ccall/36157 "2020-03-18T18:34:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 18, 2020, 6:34pm UTC](https://discourse.julialang.org/t/julia-crashes-after-successful-ccall/36157/1 "2020-03-18T18:34:14Z")

</div>

I am trying to control a National Instruments PXI oscilloscope using Julia by calling the provided `.dlls`.  
So far I managed to initialize the communication with the instrument, configure some settings and almost finished the `Fetch` function for reading waveforms. I store the waveform in a simple `Array{Float64,1}`  
The problem is, whenever I try to plot that array, or serialize it to disk, Julia crashes (I am on Windows, I don’t know how to log the crash report, but there are some errors printed on console in the second before Julia closes).  
My code is the following:

```julia
ViStatus = Int32
ViSession = UInt32 
ViConstString = Ptr{UInt8}
ViReal64 = Float64
ViInt32 = Int32

sym = Libdl.dlsym(lib, :niScope_Fetch)
channelList = [UInt8.(collect("0")); UInt8(0)] # terminate with NULL char
timeout = 0.0
npts = 1000
numSamples = Int32(npts)

struct niScope_wfmInfo
	relativeInitialX::Float64
	absoluteInitialX::Float64
	xIncrement::Float64
	actualSamples::Int32
	gain::Float64
	offset::Float64
end

wfmInfo = niScope_wfmInfo(0.0, 0.0, 0.0, Int32(0), 1.0, 0.0)
PwfmInfo = Ref(wfmInfo) # pointer to struct to be passed to ccall

wfm = Vector{Float64}(undef, npts) # arg type is pointer but one needs to pass the array

status = ccall(sym, ViStatus, 
       (ViSession, ViConstString, ViReal64, ViInt32, Ptr{Array{Float64,1}}, Ptr{niScope_wfmInfo}) ,
       scope_obj, channelList, timeout, numSamples, wfm, PwfmInfo)

plot(wfm)

```

Am I missing something?  
It’s the first time I use ccall and my experience with C is almost zero.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 18, 2020, 8:15pm UTC](https://discourse.julialang.org/t/julia-crashes-after-successful-ccall/36157/2 "2020-03-18T20:15:57Z")

</div>

> [@Iulian.Cioarca](#):
>
> Ptr{Array{Float64,1

This is definitely the wrong type since I assume NI doesn’t use Julia in its API. What exactly is the signature of the c function?

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 18, 2020, 8:26pm UTC](https://discourse.julialang.org/t/julia-crashes-after-successful-ccall/36157/3 "2020-03-18T20:26:43Z")

</div>

`ViStatus niScope_Fetch (ViSession vi, ViConstString channelList, ViReal64 timeout, ViInt32 numSamples, ViReal64* wfm, struct niScope_wfmInfo* wfmInfo);`

 ![ni_scope_fetch](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae3e764a025e44bb76a92e5b349376f8fc8765ac.png)

This is from their documentation. I don’t have the `.h` files.  
I can confirm that `wfm` contains the right data, because I can view it in REPL.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 19, 2020, 10:17am UTC](https://discourse.julialang.org/t/julia-crashes-after-successful-ccall/36157/4 "2020-03-19T10:17:53Z")

</div>

Update:  
Looking closer in the documentation shows that `wfmInfo` is actually an **array** of structs.  
This minor change fixes my issue:

> <https://github.com/iuliancioarca/GenericInstruments.jl/blob/9af3ad81ed282ae0a650b2d6af38479654053258/src/niScope.jl#L96>

I also changed the `Ptr{Array{Float64,1}` to `Ref{Cdouble}`.
