[Ironpython-users] Memory Allocation in Iron Python

Jim Pattee jimp02 at email.com
Tue Jan 24 01:27:19 CET 2012


 Attached is a test case for the Memory Allocation problem. One folder contains two python scripts and the astyle.dll. The other contains test data. I don’t know of any Windows API functions which work the same as astyle. A callback gets a memory allocation that is used to return text to the python program. The DLL is 32 bit. Therefore you must use the 32 bit python executable to run the script. Let me know if this is a problem and I will send a 64 bit version.
 There are two python scripts included. ExampleUnicode.py is the CPython script. It will work with either python version 2.7 or 3.2 (32 bit). The other, ExampleUnicode *Iron*.py, is the IronPython program. It will also work with CPython. But it doesn’t work with IronPython. I included both so you can use a diff program to see the changes. There aren’t many. I am aware of the Unicode problem with IronPython. It has been fixed and byte code is correctly being sent to the DLL.
 The call to astyle is at line 59 or 60 in the scripts. Text_in is sent to the dll, a python memory allocation function is called, and formatted_text is returned. Formatted_text uses the memory that was allocated by the python memory allocation function.
 AStyle is open source and can be obtained from http://sourceforge.net/projects/astyle/  .
 Let me know if you have questions. It is a fairly complicated process, but it works with CPython.
 And thanks for the responses. I really wasn't very optimistic about anyone understanding the problem.

Jim Pattee
 jimp02 at email.com

----- Original Message -----
From: Dino Viehland
Sent: 01/23/12 03:57 PM
To: Markus Schaber, Jim Pattee, ironpython-users at python.org
Subject: RE: [Ironpython-users] Memory Allocation in Iron Python

One potential issue here could be ASCII vs Unicode strings. Depending on wherever the copy into this memory occurs it could end up copying the Unicode string into a buffer which is sized for ASCII strings. 

From: ironpython-users-bounces+dinov=microsoft.com at python.org [mailto:ironpython-users-bounces+dinov=microsoft.com at python.org]  *On Behalf Of *Markus Schaber
 *Sent:* Sunday, January 22, 2012 11:44 PM
 *To:* Jim Pattee; ironpython-users at python.org
 *Subject:* Re: [Ironpython-users] Memory Allocation in Iron Python
Hi, Jim,
You only gave us half of the source. Which way do you pass the memory address to the native DLL?
One difference is, for example, that .NET relocates objects sometimes, so they need to be pinned.
Best regards

 Markus Schaber
We software Automation.

3S-Smart Software Solutions GmbH
Markus Schaber | Developer
 Memminger Str. 151 | 87439 Kempten | Germany | Tel. +49-831-54031-0 | Fax +49-831-54031-50

 Email:  m.schaber at 3s-software.com  | Web: http://www.3s-software.com  http://www.3s-software.com/ 
 CoDeSys internet forum: http://forum.3s-software.com http://forum-en.3s-software.com/ 
 Download CoDeSys sample projects: http://www.3s-software.com/index.shtml?sample_projects 

Managing Directors: Dipl.Inf. Dieter Hess, Dipl.Inf. Manfred Werner | Trade register: Kempten HRB 6186 | Tax ID No.: DE 167014915
Von: ironpython-users-bounces+m.schaber=3s-software.com at python.org  %5Bmailto:ironpython-users-bounces+m.schaber=3s-software.com at python.org%5D  *Im Auftrag von *Jim Pattee
 *Gesendet:* Montag, 23. Januar 2012 00:50
 *An:* ironpython-users at python.org 
 *Betreff:* [Ironpython-users] Memory Allocation in Iron Python
I am trying to allocate memory in Iron Python to receive text from a DLL. The allocated memory address is passed to the DLL and the text is copied by the DLL into the memory area. The following code, which works in CPython, is used:
allocated =[]
def MemoryAllocation(size):
 arr_type = c_char * size # create a c_char array
 arr_obj = arr_type()# create an array object
 allocated.append(arr_obj)# so the object will not be destroyed
if len(allocated)> 1: # free memory for the previous object
del allocated[0]
return addressof(arr_obj)# return a pointer
When this is used for Iron Python it results in:
“Unhandled Exception: System.AccessViolationException: Attempted to read or write
protected memory. This is often an indication that other memory is corrupt.”
The error results when the memory is copied into Python by the DLL. There are no errors displayed when executing the above code.
Why can it not write to the memory? What is the difference in Iron Python and CPython?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120123/25743404/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PythonTest.zip
Type: application/zip
Size: 160824 bytes
Desc: Attachment: PythonTest.zip
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120123/25743404/attachment-0001.zip>

More information about the Ironpython-users mailing list