Re: [ctypes-users] Can ctypes free memory that be malloced in so
Brought to you by:
theller
From: Thomas H. <th...@ct...> - 2007-03-01 20:35:57
|
Scott Dial schrieb: > feihu_roger wrote: >> Thanks, I do as your suggest. >> >> When on windows, after import ctypes, msvcrt is auto loaded. I can directly >> call: >> ctypes.cdll.msvcrt71.free(xxxx) >> >> When on Linux, I must manually load libc >> a=CLDD('libc.so.6) or a=cdll.LoadLibrary('libc.so.6') >> a.free(xxxx) >> > > If you care about portability, the general solution for getting at libc > has decidedly been: > > import re > import sys > from ctypes import * > > def find_msvcr(): > bytes = open(sys.executable, "rb").read() > match = re.search("msvcr([0-9]+|t).dll", bytes, re.IGNORECASE) > return match.group(0) > > if sys.platform == 'win32': > libc = CDLL(find_msvcr()) > elif sys.platform in ('linux2', 'freebsd6'): > libc = CDLL('libc.so.6') > elif sys.platform == 'darwin': > libc = CDLL('libc.dylib') > else: > raise AssertionError("don't know how to get to libc for %s" > % sys.platform) > This is correct for Windows. For linux and OS X (I'm not sure about other platforms), it may even be enough to load 'CDLL(None)'. The libc library is already loaded by Python, and posix exposes all the functions from a global namespace. AFAIK. Thomas |