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


Groups > comp.lang.python > #32282

Re: ctypes free memory which is allocated in C DLL

References <e5ab59f8-da39-4d50-bca2-63a5f5ec0cc2@googlegroups.com>
Date 2012-10-28 01:56 +1100
Subject Re: ctypes free memory which is allocated in C DLL
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2940.1351349811.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

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