Re: freeing memory of ctypes python variables (LukenShiro)

Patricio Stegmann <[email protected]> Tue, 11 Jan 2011 14:26:48 -0200
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
LukenShiro,

Even using del l__buf, l__podata it wont free the memory.
The problem appears when I create a pointer to my variable. It doesnt free the memory. If no pointer is created, the memory is freed as soon as the function returns. Someone pointed me on usging gc.collect() ... however this seems an inelegant solution, and I suppose ctypes has something better to do that.

> From: [email protected]
> Subject: ctypes-users Digest, Vol 56, Issue 1
> To: [email protected]
> Date: Tue, 11 Jan 2011 12:07:52 +0000
> 
> Send ctypes-users mailing list submissions to
> 	[email protected]
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	https://lists.sourceforge.net/lists/listinfo/ctypes-users
> or, via email, send a message with subject or body 'help' to
> 	[email protected]
> 
> You can reach the person managing the list at
> 	[email protected]
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of ctypes-users digest..."
> 
> 
> Today's Topics:
> 
>    1. freeing memory of ctypes python variables (Patricio Stegmann)
>    2. Re: freeing memory of ctypes python variables (LukenShiro)
>    3. pyusb under Cygwin and stdcall libusb-1.0 (Xiaofan Chen)
>    4. Re: pyusb under Cygwin and stdcall libusb-1.0
>       [SEC=UNCLASSIFIED] (Andrew MacIntyre)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Mon, 10 Jan 2011 17:31:13 -0200
> From: Patricio Stegmann <[email protected]>
> Subject: [ctypes-users] freeing memory of ctypes python variables
> To: ctypes users mailing list <[email protected]>
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> 
> Hello to all,
> 
> I am looking for a way to free the memory of some variables on a ctypes wrapper I have.
> I saw that if the code is using python variables, the python's gc should free the memory. So for example if I create a buffer (via ctypes.create_string_buffer(100*1024*1024)) and return from that function, python will free automagically that memory.
> 
> See the above example which works fine:
> 
> """
> import ctypes
> 
> def do_buffer():
>     l__buf = ctypes.create_string_buffer(100*1024*1024)
> 
> def prompt_user():
>     l__res = ''
>     while l__res != 'x':
>         do_buffer()
>         l__res = raw_input('press x to exit or other to re-get mem')
> 
> if __name__ == '__main__':
>     prompt_user()
> """
> 
> However if I create a pointer to that buffer, ctypes or python is not freeing the memory even after returning from the function. For example this new code never frees the memory:
> 
> """
> import ctypes
> 
> def do_buffer():
>     l__buf = ctypes.create_string_buffer(100*1024*1024)
>     l__podata = ctypes.cast(l__buf, ctypes.POINTER(ctypes.c_ubyte))
> 
> def prompt_user():
>     l__res = ''
>     while l__res != 'x':
>         do_buffer()
>         l__res = raw_input('press x to exit or other to re-get mem')
> 
> if __name__ == '__main__':
>     prompt_user()
> """
> 
> I wonder how can I be able to free that buffer.
> This is happening to me on Windows (xp pro), with python "Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32".
> 
> Thank you for any suggestion,
>  		 	   		  
> -------------- next part --------------
> An HTML attachment was scrubbed...
> 
> ------------------------------
> 
> Message: 2
> Date: Mon, 10 Jan 2011 22:52:35 +0100
> From: LukenShiro <[email protected]>
> Subject: Re: [ctypes-users] freeing memory of ctypes python variables
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=US-ASCII
> 
> Il giorno Mon, 10 Jan 2011 17:31:13 -0200
> Patricio Stegmann <[email protected]> ha scritto:
> > I am looking for a way to free the memory of some variables on a
> > ctypes wrapper I have. I saw that if the code is using python
> > variables, the python's gc should free the memory. So for example if
> > I create a buffer (via ctypes.create_string_buffer(100*1024*1024))
> > and return from that function, python will free automagically that
> > memory.
> [..]
> > However if I create a pointer to that buffer, ctypes or python is not
> > freeing the memory even after returning from the function.
> 
> If I'm not mistaken, I guess removing with 'del' the related
> variable(s), whose allocated memory you want to free, should speed the
> garbage collector process. If it is kept referenced I doubt it will be
> ever freed. :)
> 
> 
> -- 
> GNU/Linux * Slackware64 current
> LU #210970 SU #12583 LM #98222/#412913
> 
> 
> 
> 
> ------------------------------
> 
> Message: 3
> Date: Tue, 11 Jan 2011 10:40:12 +0800
> From: Xiaofan Chen <[email protected]>
> Subject: [ctypes-users] pyusb under Cygwin and stdcall libusb-1.0
> To: [email protected]
> Message-ID:
> 	<[email protected]>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> http://sourceforge.net/mailarchive/message.php?msg_id=26873757
> There seems to be problems to get pyusb to work under Cygwin.
> 
> The author asks if Cygwin libusb-1.0 should use stdcall or not?
> http://sourceforge.net/mailarchive/message.php?msg_id=26873777
> 
> Normal Python under Windows can support either cdecl (CDLL) or
> stdcall (WinDLL). The problem is that Cygwin Ctypes only supports
> cdecl. Therefore it is not possible to load cygusb-1.0.dll and pyusb's
> libusb-1.0 backend will not work under Cygwin as a result.
> 
> Just wondering why Ctypes under Cygwin does not support
> WinDLL? Take note os.name == 'posix' under Cygwin Python.
> I have updated Cygwin installation to the latest version.
> 
> -- 
> Xiaofan
> 
> 
> 
> ------------------------------
> 
> Message: 4
> Date: Tue, 11 Jan 2011 16:24:54 +1100
> From: "Andrew MacIntyre" <[email protected]>
> Subject: Re: [ctypes-users] pyusb under Cygwin and stdcall libusb-1.0
> 	[SEC=UNCLASSIFIED]
> To: "Xiaofan Chen" <[email protected]>,
> 	[email protected]
> Message-ID:
> 	<[email protected]>
> Content-Type: text/plain; charset=us-ascii
> 
> From: Xiaofan Chen
> 
> > Just wondering why Ctypes under Cygwin does not support
> > WinDLL? Take note os.name == 'posix' under Cygwin Python.
> 
> That really is a question for the maintainer of Python on Cygwin.  It
> may be because the necessary libffi plumbing isn't in yet place on that
> platform.
> 
> -------------------------> "These thoughts are mine alone!" <---------
> Andrew MacIntyre           Operations Branch
> tel:   +61 2 6219 5356     Communications Infrastructure Division
> fax:   +61 2 6253 3277     Australian Communications & Media Authority
> email: [email protected]            http://www.acma.gov.au/
> 
> 
> NOTICE: This email message is for the sole use of the intended recipient(s) 
>  and may contain confidential and privileged information. Any unauthorized 
>  review, use, disclosure or distribution is prohibited. If you are not the 
>  intended recipient, please contact the sender by reply email and destroy all 
>  copies of the original message.
> 
> 
> 
> ------------------------------
> 
> ------------------------------------------------------------------------------
> Gaining the trust of online customers is vital for the success of any company
> that requires sensitive data to be transmitted over the Web.   Learn how to 
> best implement a security strategy that keeps consumers' information secure 
> and instills the confidence they need to proceed with transactions.
> http://p.sf.net/sfu/oracle-sfdevnl 
> 
> ------------------------------
> 
> _______________________________________________
> ctypes-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/ctypes-users
> 
> 
> End of ctypes-users Digest, Vol 56, Issue 1
> *******************************************

------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand 
malware threats, the impact they can have on your business, and how you 
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users