Hi,
Using cast() or from_address() to convert from c_void_p (or integer) address to POINTER(c_ubyte) causes the interpreter to crash, when accessing the contents of the array.
The problem seems to happen when running the following combination:
Windows 10 @ 64 bit, version 1803 (OS build 17134.407) and python 3 64 bit, starting with 3.5 until 3.7 (the last that was tested).
The following code works fine on the same system using python 2.7.15 or python 3 until 3.4 (inclusive), 64 bit.
In addition, it works well also under Windows 7 64 bit and python 3.5/3.6/3.7 etc.
How to reproduce?
```
from ctypes import wintypes, windll, c_void_p, c_size_t, POINTER, c_ubyte, cast
# Define constants.
FILE_MAP_ALL_ACCESS = 983071
PAGE_READWRITE = 4
# Configure function arguments.
windll.kernel32.CreateFileMappingA.argtypes = [wintypes.HANDLE, c_void_p, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD, wintypes.LPCSTR]
windll.kernel32.CreateFileMappingA.restype = wintypes.HANDLE
windll.kernel32.MapViewOfFile.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD, c_size_t]
windll.kernel32.MapViewOfFile.restypes = wintypes.LPVOID
# Open shared-memory.
handle = windll.kernel32.CreateFileMappingA(-1, None, PAGE_READWRITE, 0, 1024 * 1024, b'TestSHMEM')
# Obtain pointer to SHMEM buffer.
ptr = windll.kernel32.MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 1024 * 1024)
arr = cast(ptr, POINTER(c_ubyte))
# or
#arr = POINTER(c_ubyte).from_address(ptr)
arr[0]
# now crashes
```
Also note that changing the return type of MapViewOfFile as follows:
windll.kernel32.MapViewOfFile.restypes = wintypes.LPVOID
is changed to:
windll.kernel32.MapViewOfFile.restypes = POINTER(c_ubyte)
The contents of the buffer can be directly accessed without a problem.
Just in case I'm not using it properly with new python releases?
Thanks!
Moti.
|