472,983 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 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 5959
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lebo | last post by:
Hi I'm trying to understand how Python handles memory usage and dynamic object loading and unloading. Problem to solve? Build a very low memory footprint (non-GUI) Python application for...
1
by: Srijit Kumar Bhadra | last post by:
Hello, Here are code snippets to create and access shared memory in Python with and without ctypes module. With regards, Srijit Filename : SharedMemCreate.py import msvcrt, mmap
0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
12
by: iker.arizmendi | last post by:
Is there any way to get Python to release memory back to the C allocator? I'm currently running a script that goes through the following steps: 1) Creates a very large number of Python objects...
0
by: Oliver Andrich | last post by:
Hi everybody, I am currently playing around with ctypes and a C library (libWand from ImageMagick), and as I want to easily deploy it on Mac, Linux and Windows, I prefer a ctypes solution over a...
1
by: geskerrett | last post by:
I was wondering if a ctypes expert could point me in the right direction. I am using the wrapper for the "freeimage" library to create multipage TIFF files from a group of png images. The...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
5
by: Tim | last post by:
Hello Everyone, I am getting shared memory in python using the following. szName = c_char_p(name) hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, None, PAGE_READONLY, 0,...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.