471,330 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Releasing malloc'd memory using ctypes?

I am starting to experiment with ctypes. I have a function which returns a
pointer to a struct allocated in heap memory. There is a corresponding free
function for that sort of struct, e.g.:

from ctypes import *

cdll.LoadLibrary("libthing.so")
c_thing = CDLL("libthing.so")

class THING(Structure):
_fields_ = [("name", c_char_p),
("value", c_int)]

get_thing = c_thing.get_thing
get_thing.restype = POINTER(THING)
free_thing = c_thing.free_thing

So I call get_thing() and get back this ctypes wrapper for a pointer to a
thing. I can extract the name and value elements from the thing instance
just fine:

thing_p = get_thing()
thing = thing_p.contents
print thing.name, "=", thing.value

Now I need to call free_thing. What do I pass it? thing_p? Some attribute
of thing_p? Something else altogether? The ctypes module docs seem to be
strangely silent on the question of freeing heap memory which you've
received from the underlying library's functions.

Thanks,

Skip

Dec 23 '07 #1
1 5365
sk**@pobox.com schrieb:
I am starting to experiment with ctypes. I have a function which returns a
pointer to a struct allocated in heap memory. There is a corresponding free
function for that sort of struct, e.g.:

from ctypes import *

cdll.LoadLibrary("libthing.so")
c_thing = CDLL("libthing.so")

class THING(Structure):
_fields_ = [("name", c_char_p),
("value", c_int)]

get_thing = c_thing.get_thing
get_thing.restype = POINTER(THING)
free_thing = c_thing.free_thing

So I call get_thing() and get back this ctypes wrapper for a pointer to a
thing. I can extract the name and value elements from the thing instance
just fine:

thing_p = get_thing()
thing = thing_p.contents
print thing.name, "=", thing.value

Now I need to call free_thing. What do I pass it? thing_p? Some attribute
of thing_p? Something else altogether? The ctypes module docs seem to be
strangely silent on the question of freeing heap memory which you've
received from the underlying library's functions.
You simply declare free by loading libc (I'm a unix-guy, Windows will
have an equivalent) as library and then declaring it. And of course it
gets a void-pointer, so you have to cast you pointer to void* - as you'd
have in C (or get a warning).

Diez
Dec 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by lebo | last post: by
1 post views Thread by Srijit Kumar Bhadra | last post: by
reply views Thread by Srijit Kumar Bhadra | last post: by
12 posts views Thread by iker.arizmendi | last post: by
reply views Thread by Oliver Andrich | last post: by
1 post views Thread by geskerrett | last post: by
71 posts views Thread by desktop | last post: by
5 posts views Thread by Tim | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.