Skip to content

Cleanup how we handle local references in JNI interactions#616

Merged
ktoso merged 12 commits intoswiftlang:mainfrom
madsodgaard:localref-cleanups
Mar 17, 2026
Merged

Cleanup how we handle local references in JNI interactions#616
ktoso merged 12 commits intoswiftlang:mainfrom
madsodgaard:localref-cleanups

Conversation

@madsodgaard
Copy link
Contributor

@madsodgaard madsodgaard commented Mar 12, 2026

We were hitting "JNI ERROR (app bug): local reference table overflow (max=512)".

In order to improve this, I've added some DeleteLocalRefs and explicit Push/Pop of local frames to SwiftJava where I saw fit.

These changes made our app stopping crashing with that error.

This also modifies the "implement Swift protocols in Java" generated wrappers, such that all calls are wrapped in a local frame to ensure that resources are cleaned up, as this might not be called from a JVM thread and be auto-cleaned.

@madsodgaard madsodgaard requested a review from ktoso as a code owner March 12, 2026 12:03
@ktoso ktoso self-requested a review March 12, 2026 12:53
@ktoso
Copy link
Collaborator

ktoso commented Mar 12, 2026

I'll give this a closer look actually tomorrow

@madsodgaard
Copy link
Contributor Author

@ktoso I updated to use the new frame helpers when we generate protocol wrappers. On top of that, I actually removed the frame helpers from the method calls. Those should be handled by the other DeleteLocalRefs, and were probably the ones I was most doubting the correctness of, so let's leave them out for now.

I've tested these changes on our Android devices, and it no longer crashes 👍

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Thanks, looks better now 👍 Nice to use the withLocalFrame and friends too :)

I suspect we'll have to investigate these more in the future but this looks like a good first step, thanksyou

@ktoso ktoso added the android label Mar 17, 2026
@ktoso ktoso changed the title Cleanup local refs Cleanup how we handle local references in JNI interactions Mar 17, 2026
@ktoso
Copy link
Collaborator

ktoso commented Mar 17, 2026

Renamed the PR for future changelog, sorry :)

@ktoso ktoso merged commit 80bee33 into swiftlang:main Mar 17, 2026
61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants