You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Result of multiprocessing.heap.BufferWrapper.create_memoryview can point to freed memory #85839
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2020-08-31.12:56:22.474>labels= ['ctypes', 'library', '3.9', '3.10']
title='Result of multiprocessing.heap.BufferWrapper.create_memoryview can point to freed memory'updated_at=<Date2020-08-31.12:57:18.235>user='https://bugs.python.org/EricWieser'
The full definition of multiprocessing.heap.BufferWrapper is:
classBufferWrapper(object):
_heap=Heap()
def__init__(self, size):
ifsize<0:
raiseValueError("Size {0:n} out of range".format(size))
ifsys.maxsize<=size:
raiseOverflowError("Size {0:n} too large".format(size))
block=BufferWrapper._heap.malloc(size)
self._state= (block, size)
util.Finalize(self, BufferWrapper._heap.free, args=(block,))
defcreate_memoryview(self):
(arena, start, stop), size=self._statereturnmemoryview(arena.buffer)[start:start+size]
But this means that a memoryview can be constructed that point to free'd "memory",
>>> b = BufferWrapper(10)
>>> m = b.create_memoryview()
>>> del b # free is called
>>> b[0] # making this invalid
It would be better if m would keep a reference to b so that it remains alive. RawArray works around this by placing a reference to b in ctypes_obj._wrapper, but this results in b being lost again if m2 = memoryview(ctypes_obj); del ctypes_obj is used.
Activity
EricWieser commentedon Aug 31, 2020
The full definition of
multiprocessing.heap.BufferWrapperis:But this means that a
memoryviewcan be constructed that point to free'd "memory",It would be better if
mwould keep a reference tobso that it remains alive.RawArrayworks around this by placing a reference tobinctypes_obj._wrapper, but this results inbbeing lost again ifm2 = memoryview(ctypes_obj); del ctypes_objis used.EricWieser commentedon Aug 31, 2020
ZeroIntensity commentedon Oct 21, 2026
This isn't a ctypes issue.