This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ctypes.from_buffer counterpart to actively remove the mapping
Type: behavior Stage: resolved
Components: ctypes Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: frispete, iritkatriel
Priority: normal Keywords:

Created on 2016-12-14 11:36 by frispete, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg283188 - (view) Author: Hans-Peter Jansen (frispete) * Date: 2016-12-14 11:36
In an attempt of using ctypes.from_buffer() to map a structure to a memory mapped file, it is important to destroy the mapping after use, because the mmap won't be resizable or freeable correctly until then.

The best approach, I was able to came up with is using a context manager, but calling the dtor of the mapping in __exit__ is not enough, which results to code like this:

with StructMap(ctypes_struct, mm, offest) as smap:
    smap.xxx = 'blabla'
del smap # necessary, but ugly

Without the del, the "with" variable still exist in the local context, hence the mapping still exist, until it is explicitly destroyed.

I hope, that ctypes is able to (or can be made to) actively remove the mapping in the __exit__ part of the context manager.

I've put some code on stackoverflow to play with this:

http://stackoverflow.com/questions/41077696/python-ctypes-from-buffer-mapping-with-context-manager-into-memory-mapped-file

The problem seems to exist with the linux mmap implementation only.
msg410989 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-19 23:34
Have you tried Mark Tolonen's suggestion on the stack overflow question, to set the instance variables to None in __exit__?
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73156
2022-01-29 14:25:35iritkatrielsetstatus: pending -> closed
stage: resolved
2022-01-19 23:34:08iritkatrielsetstatus: open -> pending

nosy: + iritkatriel
messages: + msg410989

resolution: not a bug
2016-12-14 11:36:48frispetecreate