Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'function,': 0.07; 'api': 0.09; 'python': 0.09; 'structure,': 0.09; 'subject:ctypes': 0.09; 'language,': 0.11; 'library': 0.15; 'allocates': 0.16; 'dll,': 0.16; 'exposes': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guys,': 0.16; 'oct': 0.16; 'structure.': 0.16; 'subject:which': 0.16; 'wrote:': 0.17; 'pointer': 0.17; 'memory': 0.18; 'written': 0.20; 'received:209.85.214.174': 0.21; 'work.': 0.23; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'wrote': 0.26; 'am,': 0.27; 'dll': 0.27; 'message-id:@mail.gmail.com': 0.27; 'interface': 0.27; "doesn't": 0.28; 'returned': 0.30; 'function': 0.30; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'received:209.85': 0.35; 'but': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'free': 0.61; 'basically,': 0.84; 'safe.': 0.93; 'subject:free': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=JEBsk9d6Lg2ACyV8F/rRV0xzzv60vH54E+Bf3n99v+A=; b=cxdDNHaosLoBJ5ItsX7X1pEMi/KiHzMuqHmykalSkafMDyP+j/+h3BDBF51wUAOtxy VZtx21DL0yCElsiN8kD1x2DGU2cDFHZiQXXZ2wO1Dy3DN3S6tPv+RGG9HNfgHquhhhWX zaCKUtqjsG/Zm9hqCPwcV2PR/REsjBC8Ru1h6R0ZanzKfPqiOr3tezuawhRFnrk5y/lr XKYAG0/lnjB50kOsWqqtZyU6kqDZA1JV4M/MHnPe3vUie/+kRsCYmxYWncKtFHX1XDuu Z7x0aFilmW5kDmad5Uivy4wNoPilkjXIRWCGcKBeFSCTbYhdVImae6fBE0PmYWQYPcXu heLg== MIME-Version: 1.0 In-Reply-To: References: Date: Sun, 28 Oct 2012 01:56:47 +1100 Subject: Re: ctypes free memory which is allocated in C DLL From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: , Newsgroups: comp.lang.python Message-ID: Lines: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351349811 news.xs4all.nl 6978 [2001:888:2000:d::a6]:45906 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32282 On Sun, Oct 28, 2012 at 1:42 AM, wrote: > Hi Guys, > > I have a DLL which written in C language, one of the function is to alloc= ate a structure, fill the members and then return the pointer of the struct= ure. > > After Python called this function, and done with the returned structure, = I would like to free the returned structure. How can I achieve this ? Basic= ally, I tried that I wrote a corresponding free interface in the DLL, it wo= rks, 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