Repository navigation
Conversation
Vect_read_area_to_wkb(), Vect_read_line_to_wkb() and Vect_line_to_wkb() returned the buffer allocated by GEOS. It has to be released with GEOSFree(), but the Python code in pygrass released it with G_free(). That works when all libraries share one C runtime, as on Linux. When GEOS and GRASS use different runtimes, as on Windows, it corrupts the heap and the process crashes (test_pygrass_vector_doctests.py stopped at areas_to_wkb_list()). The WKT functions in the same file already copy the GEOS result into memory from G_store() and free the original. Copy the buffer into memory from G_malloc() and release the GEOS one with GEOSFree(), so G_free() is correct for the caller. Document this. Release the buffer in Line.to_wkb() and Area.to_wkb() in pygrass, which never freed it. Calling Line.to_wkb() 60000 times grew the memory by 4.7 MB before and by nothing after the change. The changes were developed with the help of an AI assistant (Claude).
Add pytest tests for the WKB functions of pygrass vector. The checks run in a subprocess in a GRASS session, as the C libraries can be initialized only once per process, and a crash then fails one test. - Line.to_wkb() and Area.to_wkb() release the buffer with G_free(). - The functions returning lists release each buffer with G_free(). - Many calls do not leave memory allocated (glibc only). - All functions return WKB of the expected size when called repeatedly. This is the case which crashes on Windows when a buffer is released with the wrong allocator. Without the change to the vector library and pygrass, the checks for the buffers of Line.to_wkb() and Area.to_wkb() and for the memory fail. The tests were developed with the help of an AI assistant (Claude).
echoix
force-pushed
the
vector-wkb-free
branch
from
October 14, 2026 16:46
56a37b0 to
2ef2927
Compare
The preview rule PLR6201 of ruff, which the Python code quality check applies, asks for a set literal. The change was developed with the help of an AI assistant (Claude).
ctypes.CDLL(None) raises a TypeError on Windows, so the check failed instead of being skipped. Test for Linux first. The change was developed with the help of an AI assistant (Claude).
4 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Vect_read_area_to_wkb(),Vect_read_line_to_wkb()andVect_line_to_wkb()returned the buffer allocated by GEOS. It has to be released withGEOSFree(), but the Python code in pygrass released it withG_free(). That works when all libraries share one C runtime, as on Linux. When GEOS and GRASS use different runtimes, as on Windows, it corrupts the heap. On Windows,test_pygrass_vector_doctests.pystops without a traceback after seven tests, inareas_to_wkb_list(), and has been failing onmain.The WKT functions in the same file already copy the GEOS result into memory from
G_store()and free the original, so this makes the WKB functions consistent with them.G_malloc()and release the GEOS one withGEOSFree().G_free()is then correct for the caller. The return value is documented as such. No C code calls these functions.Line.to_wkb()andArea.to_wkb()in pygrass never freed the buffer. Free it after copying the bytes.Motivation and context
Heap corruption on Windows, and a memory leak on all platforms.
How has this been tested?
New pytest tests in
python/grass/pygrass/vector/tests/. Each check runs in a subprocess in a GRASS session, so a crash fails one test:Line.to_wkb()andArea.to_wkb()release the buffer withG_free().G_free().mallinfo2, skipped elsewhere).On Linux, without the change the first, second and memory checks fail (no
G_free()call, 6.1 MB left allocated after 20000 calls), and with the change all pass. The Windows crash cannot be reproduced on Linux, so the Windows job of this PR is the check for it: the last test andtest_pygrass_vector_doctests.pyshould pass there.Also on Linux: the pygrass vector doctests and the 7 test files in
python/grass/pygrass/vectorpass withgrass.gunittest.main;MALLOC_CHECK_=3reports no heap errors.clang-format,ruff checkandruff formatpass.Types of changes
functionality to not work as before)
Checklist
pre-defined prefixes
of this project.
The changes were developed with the help of an AI assistant (Claude).