Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ffi): add OwnedString class #438

Merged
merged 1 commit into from
Oct 26, 2023
Merged

feat(ffi): add OwnedString class #438

merged 1 commit into from
Oct 26, 2023

Conversation

JP-Ellis
Copy link
Contributor

📝 Summary

  • Add an OwnedString class
  • Update type annotations for functions which return strings which must be freed.

🚨 Breaking Changes

🔥 Motivation

A number of FFI functions return strings that are owned by the library and must be freed manually. Unfortunately, returning a str from a function would result in a memory leak, as the Python runtime would not know to free the string.

This commit adds an OwnedString class that wraps a str and a function that frees the string. The __del__ method of the class calls the free function, ensuring that the string is freed when the object is garbage collected.

🔨 Test Plan

  • Unit tests have been included

🔗 Related issues/PRs

@JP-Ellis JP-Ellis added type:feature New feature difficulty:easy A simple task appropriate for newcomers to the codebase area:v3 Relating to the pact.v3 module labels Oct 26, 2023
@JP-Ellis JP-Ellis self-assigned this Oct 26, 2023
A number of FFI functions return strings that are owned by the library
and must be freed manually. Unfortunately, returning a `str` from a
function would result in a memory leak, as the Python runtime would not
know to free the string.

This commit adds an `OwnedString` class that wraps a `str` and a
function that frees the string. The `__del__` method of the class calls
the free function, ensuring that the string is freed when the object is
garbage collected.

Signed-off-by: JP-Ellis <[email protected]>
@JP-Ellis JP-Ellis merged commit 97dfcb9 into master Oct 26, 2023
18 of 29 checks passed
@JP-Ellis JP-Ellis deleted the feat/owned-string branch October 26, 2023 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:v3 Relating to the pact.v3 module difficulty:easy A simple task appropriate for newcomers to the codebase type:feature New feature
Projects
Status: ✅ Completed
Development

Successfully merging this pull request may close these issues.

Add OwnedString to FFI
1 participant