Julia crashes after successful(?) ccall

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:

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

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)


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

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?

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

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.

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

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

1 Like