I have a problem that when using in a ctypes (python) structure c_char_p
, after having saved the structure in the file and finished the code. When the structure is read with readinto
and the attribute signed with c_char_p
is called, segmentation fault
appears.
Code:
import ctypes
class A(ctypes.Structure):
_pack_ = 1
_fields_ = [("x", ctypes.c_char_p)]
test_char = b"Hello world"
# line 13-18
with open("test.struct", "wb+") as f:
root = A(test_char)
f.write(root)
print(root.x)
del root
f.close() # ignore
# test 1
with open("test.struct", "rb+") as f:
lii = A()
f.readinto(lii)
# Segmentation Fault when trying to convert bytes to c_char_p
print(lii.x)
f.close()
del lii
# test 2
with open("test.struct", "rb+") as f:
lii = A()
f.readinto(lii)
print(lii.x)
f.close()
del lii
So far everything works fine, But let's delete from line 13 to 18 of this file: The result is segmentation fault in print(lii.x)
.
In some cases when the signature is changed to c_wchar_p
it gives an invalid range of unicode characters, making it known that there is a bit mess when SAVING the structure in the file.
When using c_char
or c_wchar
it works perfect.
Is there a solution or did I something wrong?