Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32284

Re: ctypes free memory which is allocated in C DLL

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 <zlchen.ken@gmail.com>
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 <mailman.2940.1351349811.27098.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=118.135.15.148; posting-account=EUV6tAoAAAA_IfJF_x-XYQxCI2bc4CDF
References <e5ab59f8-da39-4d50-bca2-63a5f5ec0cc2@googlegroups.com> <mailman.2940.1351349811.27098.python-list@python.org>
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 <zlchen.ken@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.2941.1351352453.27098.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Saturday, October 27, 2012 10:56:54 PM UTC+8, Chris Angelico wrote:
> On Sun, Oct 28, 2012 at 1:42 AM,  <zlchen.ken@gmail.com> wrote:
> 
> > Hi Guys,
> 
> >
> 
> > I have a DLL which written in C language, one of the function is to allocate a structure, fill the members and then return the pointer of the structure.
> 
> >
> 
> > After Python called this function, and done with the returned structure, I would like to free the returned structure. How can I achieve this ? Basically, I tried that I wrote a corresponding free interface in the DLL, it works, but calling the libc.free in Python doesn't work.
> 
> 
> 
> As a general rule, you should always match your allocs and frees. Use
> 
> the same library to free the memory as was used to allocate it.
> 
> Nothing else is safe. If your DLL exposes an API that allocates
> 
> memory, your DLL should expose a corresponding API to free it. So what
> 
> you have is the best way :)
> 
> 
> 
> ChrisA

Thanks a lot, Chris!
Yes, I agree writing a corresponding API to free the memory is the best practice 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() 
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 determined, None is returned.

If you need to free memory, for example, allocated by an extension module with a call to the free(void *), it is important that you use the function in the same library that allocated the memory."

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

ctypes free memory which is allocated in C DLL zlchen.ken@gmail.com - 2012-10-27 07:42 -0700
  Re: ctypes free memory which is allocated in C DLL Chris Angelico <rosuav@gmail.com> - 2012-10-28 01:56 +1100
    Re: ctypes free memory which is allocated in C DLL Ken Chen <zlchen.ken@gmail.com> - 2012-10-27 08:40 -0700
      Re: ctypes free memory which is allocated in C DLL Chris Angelico <rosuav@gmail.com> - 2012-10-28 07:26 +1100
    Re: ctypes free memory which is allocated in C DLL Ken Chen <zlchen.ken@gmail.com> - 2012-10-27 08:40 -0700
  Re: ctypes free memory which is allocated in C DLL Nobody <nobody@nowhere.com> - 2012-10-27 23:26 +0100
    Re: ctypes free memory which is allocated in C DLL zlchen.ken@gmail.com - 2012-10-27 19:05 -0700

csiph-web