How do I pass a pointer to a StuctRef as an argument to a function call?

I have a c++ function called fmi2NewDiscreteStates from a DLL, which I want to call inside a jitted python function. The function takes the argument fmi2EventInfo, which is a struct that is defined using ctypes like this:

class fmi2EventInfo(Structure):

    _fields_ = [('newDiscreteStatesNeeded',           fmi2Boolean),
                ('terminateSimulation',               fmi2Boolean),
                ('nominalsOfContinuousStatesChanged', fmi2Boolean),
                ('valuesOfContinuousStatesChanged',   fmi2Boolean),
                ('nextEventTimeDefined',              fmi2Boolean),
                ('nextEventTime',                     fmi2Real)]

The datatypes (fmi2Bool, fmi2Real) are aliases for int and double, respectively.

Now the question:
How can I pass the pointer inside the jitted function? Basically, I want:

fmi2NewDiscreteStates(component, eventInfo_ptr)

I’ve tried to pass by a reference:

# Assuming I have "component" already
eventInfo = fmi2EventInfo()
eventInfo_ptr = ctypes.addressof(eventInfo)

def fmi2NewDiscreteStates_jitted():
    fmi2NewDiscreteStates(component, eventInfo_ptr)

But If I run this multiple times, I get segfault. So it seems I am accessing something that I shouldn’t. What is the correct way to do this?

Not exactly in response to the OP, but if at all useful the following works


typedef struct S {
    int x;
} S;

int get_x(S* sp) {
    return sp->x;

compiled into and loaded into

import ctypes
import numba as nb

class S(ctypes.Structure):
    _fields_ = [
        ('x', ctypes.c_int)

s = S(141)
s_p = ctypes.addressof(s)

lib1 = ctypes.cdll.LoadLibrary('./')
get_x = lib1.get_x
get_x.argtypes = (ctypes.c_void_p,)

def func():
    return get_x(s_p)

if __name__ == '__main__':
    assert S.from_address(s_p).x == get_x(ctypes.pointer(s))
    assert S.from_address(s_p).x == func()