Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'explicitly': 0.04; 'builtin': 0.07; 'filename': 0.07; 'function,': 0.07; 'returned.': 0.07; 'api': 0.09; 'python': 0.09; 'modules.': 0.09; 'structure,': 0.09; 'subject:ctypes': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'language,': 0.11; 'extension': 0.13; 'library': 0.15; '*),': 0.16; 'allocates': 0.16; 'dll,': 0.16; 'exposes': 0.16; 'guys,': 0.16; 'msvcrt': 0.16; 'oct': 0.16; 'structure.': 0.16; 'subject:which': 0.16; 'wrote:': 0.17; 'pointer': 0.17; 'memory': 0.18; 'windows': 0.19; 'load': 0.19; 'module': 0.19; 'written': 0.20; 'cc:2**0': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'wrote': 0.26; 'skip:" 20': 0.26; 'am,': 0.27; 'dll': 0.27; 'interface': 0.27; "doesn't": 0.28; 'chris': 0.28; 'manual': 0.29; 'that.': 0.30; 'returned': 0.30; 'function': 0.30; 'figure': 0.30; 'getting': 0.33; 'allocated': 0.33; 'another': 0.33; 'version': 0.34; 'agree': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'third': 0.34; 'thanks': 0.34; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'october': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'nothing': 0.38; 'called': 0.39; 'your': 0.60; 'below,': 0.60; 'free': 0.61; 'provide': 0.62; 'different': 0.63; 'finally': 0.66; 'stated': 0.69; 'basically,': 0.84; 'only:': 0.84; 'safe.': 0.93; 'lot,': 0.95; 'subject:free': 0.95 Newsgroups: comp.lang.python Date: Sat, 27 Oct 2012 08:40:49 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=118.135.15.148; posting-account=EUV6tAoAAAA_IfJF_x-XYQxCI2bc4CDF References: User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 118.135.15.148 MIME-Version: 1.0 Subject: Re: ctypes free memory which is allocated in C DLL From: Ken Chen To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351352453 news.xs4all.nl 6855 [2001:888:2000:d::a6]:52833 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32284 On Saturday, October 27, 2012 10:56:54 PM UTC+8, Chris Angelico wrote: > On Sun, Oct 28, 2012 at 1:42 AM, wrote: >=20 > > Hi Guys, >=20 > > >=20 > > I have a DLL which written in C language, one of the function is to all= ocate a structure, fill the members and then return the pointer of the stru= cture. >=20 > > >=20 > > After Python called this function, and done with the returned structure= , I would like to free the returned structure. How can I achieve this ? Bas= ically, I tried that I wrote a corresponding free interface in the DLL, it = works, but calling the libc.free in Python doesn't work. >=20 >=20 >=20 > As a general rule, you should always match your allocs and frees. Use >=20 > the same library to free the memory as was used to allocate it. >=20 > Nothing else is safe. If your DLL exposes an API that allocates >=20 > memory, your DLL should expose a corresponding API to free it. So what >=20 > you have is the best way :) >=20 >=20 >=20 > ChrisA Thanks a lot, Chris! Yes, I agree writing a corresponding API to free the memory is the best pra= ctice and best bet. Sometimes, the third party API may not provide that. After digging the Python manual again and again. I finally figure out why windll.msvcrt.free is failing. As the manual stated below, the DLL is using another version of msvcrt lib = which is different than the builtin windll.msvcrt. After I explicitly load = the msvcrt which built the DLL, things are getting function now. "ctypes.util.find_msvcrt()=20 Windows only: return the filename of the VC runtype library used by Python,= and by the extension modules. If the name of the library cannot be determi= ned, None is returned. If you need to free memory, for example, allocated by an extension module w= ith a call to the free(void *), it is important that you use the function i= n the same library that allocated the memory."