473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Ctypes and freeing memory

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 have to do the following

char *description;
long severity;

description = MagickGetException(wand, &severity);
/*
do something with the description: print it, log it, ...
*/
description = (char *) MagickRelinquishMemory(description);
exit(-1); /* or something else what I want to after an exception occured */

So, this looks easy and is sensible from C's point of view. Now I try
to translate this to Python and ctypes.

dll.MagickGetException.argtypes = [c_long, POINTER(c_long)]
dll.MagickGetException.restype = c_char_p

severity = c_long()
description = dll.MagickGetException(byref(severity))

# So far so good. The above works like a charm, but the problem follows now
# ctypes already converted the char * for description to a String object.
# That means description has arrived in an area under Python's control.

# these definitions are the easy part
dll.MagickRelinquishMemory.argtypes = [c_void_p]
dll.MagickRelinquishMemory.restype = c_char_p

# but this obviously must cause problems and causes problems
dll.MagickRelinquishMemory(description)

So, my question is, how do I deal with this situation? Can I ignore
the call to MagickRelinquishMemory, cause Python takes care of the
resources already? Or is it impossible to use it at all, and I have to
think about a different solution?

Best regards,
Oliver

--
Oliver Andrich <ol************@gmail.com--- http://roughbook.de/
Oct 3 '06 #1
0 1268

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
3
by: chris | last post by:
What is the proper way to use ctypes to access an exported Function in a dll file on windows? I must be missing something because I get memory leaks when I use it: import ctypes import gc ...
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...
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...
9
by: jtravs | last post by:
Hi all, I suspect that I'm doing something stupid, I would like some other opinions though. I'm getting started with ctypes and am trying to use distutils to help build my module. At the moment...
7
by: p.lavarre | last post by:
How do I vary the byte offset of a field of a ctypes.Structure? How do I "use the dynamic nature of Python, and (re-)define the data type after the required size is already known, on a case by...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
1
by: skip | last post by:
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.: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.