Skip to content

Free the string or byte buffer MEOS hands the caller once it is copied - #47

Merged
estebanzimanyi merged 1 commit into
MobilityDB:masterfrom
estebanzimanyi:gen/free-copied-returns
Nov 2, 2026
Merged

estebanzimanyi merged 1 commit into
MobilityDB:masterfrom
estebanzimanyi:gen/free-copied-returns

Conversation

@estebanzimanyi

Copy link
Copy Markdown
Member

A MEOS function returning char * hands the caller a string the caller
owns, as MEOS's example 03_berlinmod_assemble.c frees the
temporal_as_hexwkb it writes, so
the wrapper copies it into a Python str and frees it (_take_string); a
const char * names a string MEOS keeps, such as temporal_subtype's, and
is copied alone (_read_string). A byte buffer MEOS sizes through a
size_t pointer is copied into bytes and freed the same way
(_take_bytes). A NULL return is None before any copy. The cdef declares
the C library's free beside the catalog's functions. GoMEOS frees each
char * return and leaves each const char * alone, and MEOS.NET's string
marshalling frees the string it copies.

Witness: a wrapper returning _ffi.string(result).decode(...) or
bytes(_ffi.buffer(result, size)) left the C allocation behind on every
call: 152 string returns, 48 byte buffers and text_to_cstring.

Measured: on wheels generated from one catalog of MobilityDB master
9c0caecd72, before and after, 200000 calls each grow the resident set by
15616 KB (temporal_out), 18688 KB (temporal_as_hexwkb) and 12416 KB
(temporal_as_wkb) before and by 0 KB after; the generated surfaces
differ in those returns and the three helpers alone; PyMEOS master
passes 4855 tests with 1 skipped on the wheel after, and the smoke test
reads a freed and a kept string twice each.

A MEOS function returning char * hands the caller a string the caller
owns, as MEOS's example 03_berlinmod_assemble.c frees the
temporal_as_hexwkb it writes, so
the wrapper copies it into a Python str and frees it (_take_string); a
const char * names a string MEOS keeps, such as temporal_subtype's, and
is copied alone (_read_string). A byte buffer MEOS sizes through a
size_t pointer is copied into bytes and freed the same way
(_take_bytes). A NULL return is None before any copy. The cdef declares
the C library's free beside the catalog's functions. GoMEOS frees each
char * return and leaves each const char * alone, and MEOS.NET's string
marshalling frees the string it copies.

Witness: a wrapper returning _ffi.string(result).decode(...) or
bytes(_ffi.buffer(result, size)) left the C allocation behind on every
call: 152 string returns, 48 byte buffers and text_to_cstring.

Measured: on wheels generated from one catalog of MobilityDB master
9c0caecd72, before and after, 200000 calls each grow the resident set by
15616 KB (temporal_out), 18688 KB (temporal_as_hexwkb) and 12416 KB
(temporal_as_wkb) before and by 0 KB after; the generated surfaces
differ in those returns and the three helpers alone; PyMEOS master
passes 4855 tests with 1 skipped on the wheel after, and the smoke test
reads a freed and a kept string twice each.
@estebanzimanyi
estebanzimanyi merged commit 65d8b90 into MobilityDB:master Nov 2, 2026
4 checks passed
@estebanzimanyi
estebanzimanyi deleted the gen/free-copied-returns branch November 2, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant