473,832 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.LoadLibrar y("libthing.so" )
c_thing = CDLL("libthing. so")

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

get_thing = c_thing.get_thi ng
get_thing.resty pe = POINTER(THING)
free_thing = c_thing.free_th ing

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.content s
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 6134
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.LoadLibrar y("libthing.so" )
c_thing = CDLL("libthing. so")

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

get_thing = c_thing.get_thi ng
get_thing.resty pe = POINTER(THING)
free_thing = c_thing.free_th ing

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.content s
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
3052
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 Unix/Windows. Why use Python? End user flexibility. So far it seems that (on Windows): (1) memory increases when you import <module>. Expected behaviour. (2) memory does not release when you del <module>. Why not? Is Python
1
7679
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
4414
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 SharedMemAccess_Mutex_ctypes.py or SharedMemAccess_Mutex_win32all.py
5
4442
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 MBM http://mbm.livewiredev.com/ can be used. Both of the mentioned apps expose data got from the hardware in a shared memory area.
12
3165
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 to produce a relatively small data structure that sits in a C extension. The Python objects consume quite a bit of memory. 2) Releases all the Python objects.
0
1306
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 C module. At least on windows, I would have resource problems to compile the C module. So, ctypes is the perfect choice for me. But I am currently encountering a pattern inside the C library, that has to be used to free memory. Coding in C I...
1
2085
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 images load and save fine, the problem seems to be that the memory used during the bitmap creation isn't being released properly until the python program ends and python exits. The multipage tiff's are created in a loop (ie, creating 3 tiffs from a
71
19152
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 problem? I cannot see why using malloc instead of new does not give the same result.
5
5281
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, TABLE_SHMEMSIZE, szName) if (hMapObject == 0): print "OpenKey: Could not open name file mapping object" raise WinError()
0
9795
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10780
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10498
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.