Reputation: 18595
There is a libx.so
which export 2 functions, and a struct
,
typedef struct Tag {
int num;
char *name;
}Tag;
Tag *create(int n, char *n)
{
Tag *t = malloc(sizeof(Tag));
t->num = n;
t->name = n;
return t;
}
void use(Tag *t)
{
printf("%d, %s\n", t->num, t->name);
}
I want to call create
in Python and then save the Tag *res
returned by create
, later I will call use
and pass the Tag *res
saved before to use
, here is it (just to demonstrate):
>>>libx = ctypes.CDLL("./libx.so")
>>>res = libx.create(c_int(1), c_char_p("a"))
>>>libx.use(res)
The above code might be wrong, just to demonstrate what I want to do.
And my problem is that, how could I save the result returned by create
? Because it returns a pointer to a user-defined struct
, and I don't want to construct struct Tag
's counterpart in Python, would c_void_p
do the trick?
UPDATE
From @David's answer, I still don't quite understand one thing:
the pointer (
c_char_p("a")
) is only valid for the duration of the call tocreate
. As soon as create returns then that pointer is no longer valid.
And I assign c_char_p("a")
to t->name
in create
, when the call to create
finishes, is t->name
a dangling pointer? Because according to the quoted words, that pointer is no longer valid after create
. Why c_char_p("a")
is no longer valid?
Upvotes: 6
Views: 1059
Reputation: 44250
Python does reference counting. You'll have to use Py_INCREF() and friends for objects that are returned from "external" libraries.
UPDATE: I don't know about .so loading by python, maybe the method proposed by @David Hefferman does this automagically.
UPDATE2: delete me!
Upvotes: 0
Reputation: 613432
The C code that you present is simply not going to work. You need to be much more precise about which party allocates and is responsible for the heap memory.
In your current example you pass c_char_p("a")
to the C code. However, the pointer to that ctypes memory is only valid for the duration of the call to create
. As soon as create
returns then that pointer is no longer valid. But you took a copy of the pointer inside create
. Thus the subsequent call to use
is liable to fail.
You are going to need to take a copy of the contents of that string and store it in the struct. If you do that then you can use libx.create.restype = c_void_p
safely.
But if you want the memory you allocated to be deallocated you will have to provide a destroy
function to match the create
function. With these changes the C code would look like this:
Tag *create(int n, char *s)
{
Tag *t = malloc(sizeof(Tag));
t->num = n;
t->name = strdup(s);
return t;
}
void destroy(Tag *t)
{
free(t->name);
free(t);
}
The Python code would look like this:
libx = ctypes.CDLL("./libx.so")
libx.create.restype = c_void_p
res = libx.create(c_int(1), c_char_p("a"))
libx.use(res)
libx.destroy(res)
Upvotes: 4