Skip to content

Add OwnedBytes to transfer owned buffers through djinni, use for Lines#916

Open
matzf wants to merge 2 commits intomainfrom
feature/owned-bytes
Open

Add OwnedBytes to transfer owned buffers through djinni, use for Lines#916
matzf wants to merge 2 commits intomainfrom
feature/owned-bytes

Conversation

@matzf
Copy link
Contributor

@matzf matzf commented Feb 19, 2026

OwnedBytes transfers ownership over buffer to callee.
When received in C++, convert back to a unique_ptr; otherwise, e.g. in
swift, destroy with OwnedBytesDestructer::free.

Use for setLines, avoid one copy for OpenGL. Note that the line buffers can be are very large on e.g. swisstopo basemap (very roughly on the order of a hundred megabytes).

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f1e7b7383a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

std::free((void*)bytes.address);
return std::make_pair(nullptr, 0);
}
return std::make_pair(std::unique_ptr<T>(reinterpret_cast<T*>(bytes.address)), bytes.elementCount);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Use array-aware deleter for OwnedBytes buffers

toUniquePtr wraps a buffer containing elementCount elements in std::unique_ptr<T> with the default single-object deleter, so normal line buffers (elementCount > 1) are released with scalar delete even though they were allocated as a contiguous block via the vector allocator. This is undefined behavior (ASan reports new-delete-type-mismatch) and can corrupt heap state when LineGroup2dOpenGl::setLines drops or replaces these buffers.

Useful? React with 👍 / 👎.

Comment on lines +25 to +27
guard let pointer = UnsafeRawPointer(bitPattern: Int(ownedBytes.address)),
ownedBytes.elementCount > 0
else { return nil }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Free owned native bytes on early-return path

This guard returns nil without calling MCOwnedBytesDestructor.free, but MCOwnedBytes is ownership-transferring and must always be released. A reachable case is OwnedBytesHelper::fromVector producing elementCount == 0 with a non-null address when a vector has reserved capacity but no emitted vertices (for example degenerate lines), which causes repeated native-memory leaks on iOS in this branch.

Useful? React with 👍 / 👎.

@matzf matzf force-pushed the feature/owned-bytes branch 4 times, most recently from 4ac02b9 to 12e7e66 Compare February 23, 2026 07:32
OwnedBytes transfers ownership over buffer to callee.
When received in C++, convert back to a unique_ptr; otherwise, e.g. in
swift, destroy with OwnedBytesDestructer::free.
@matzf matzf force-pushed the feature/owned-bytes branch from 12e7e66 to a847021 Compare February 25, 2026 14:43
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