sarema
sarema

Reputation: 725

Python 3 ctypes call for function with returned struct fails with segfault

I'm trying to run a C function that returns a struct containing multiple int (for now).

My C source looks like this:

struct my_struct {
    int i;
    int j;
} ;

extern "C" struct my_struct struct_test(){
    struct my_struct s;
    s.i = 2;
    s.j = 331;
    return s;
}

The corresponding Makefile is this:

CXX=/usr/bin/g++
PYTHON=/usr/bin/ipython3 --colors Linux

CXXFLAGS=-Wall -fPIC -O3 -c 
LDFLAGS=-Wall -shared -Wl,-soname,libfoo.so

TARGETS=mylib.so

all: mylib.so test

mylib.so: mylib.cpp
    $(CXX) $(CXXFLAGS) -o mylib.o mylib.cpp
    $(CXX) $(LDFLAGS) -o mylib.so mylib.o 

test: mylib.so
    $(PYTHON) ctypes_call_test.py

clean:
    rm -f $(TARGETS)

And the Python script is here:

import ctypes

libname = './mylib.so'

class my_struct(ctypes.Structure):
    _fields_ = [
        ("i", ctypes.c_int),
        ("j", ctypes.c_int),
    ]
mylib = ctypes.cdll.LoadLibrary(libname)
mylib.struct_test.argtypes=[]
mylib.struct_test.restype=ctypes.POINTER(my_struct)
ret = mylib.struct_test();
print('got return value')
print(ret.contents.i, ret.contents.j)

When I run the Python script, it crashes immediately after the print() statement. Any ideas why this is happening?

Upvotes: 2

Views: 614

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140266

You're specifying a pointer on your structure in your return value whereas it is the structure itself.

Change the last lines to:

mylib.struct_test.restype = my_struct  # <=== no more pointer
ret = mylib.struct_test();
print('got return value')
print(ret.i, ret.j)     # <=== remove "contents"

prints:

got return value
(2, 331)

Upvotes: 6

Related Questions