Skip to content

Dispose JVM return values from function callbacks - #1040

Merged
masesdevelopers merged 3 commits into
masterfrom
1039-all-developed-javautilfunction-classes-does-not-contains-the-dispose-delegate
May 8, 2026
Merged

masesdevelopers merged 3 commits into
masterfrom
1039-all-developed-javautilfunction-classes-does-not-contains-the-dispose-delegate

Conversation

@masesdevelopers

@masesdevelopers masesdevelopers commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Description

Add optional disposal callbacks to Java.Util.Function delegates and ensure temporary JVM objects are disposed in JavaUtilExtensions.

  • Introduces OnApplyDispose / OnGetDispose properties (System.Action) to BiFunction, BinaryOperator, DoubleFunction, Function, IntFunction, LongFunction, Supplier, and UnaryOperator. These are invoked after SetReturnValue to allow immediate disposal of JVM-backed return objects (defaults to null, invoked via null-conditional).
  • Updates MASES.JNet.Specific.Extensions.JavaUtilExtensions to dispose intermediate JVM objects created during collection/map conversions: uses try/finally to Dispose temporary jvmData, keys and values, and adds using for keySet where appropriate to avoid leaking JVM global references.

This prevents leaking JVM global references by releasing them immediately when CLR-side code no longer needs the returned JVM objects.

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Add optional disposal callbacks to Java.Util.Function delegates and ensure temporary JVM objects are disposed in JavaUtilExtensions.

- Introduces OnApplyDispose / OnGetDispose properties (System.Action<T>) to BiFunction, BinaryOperator, DoubleFunction, Function, IntFunction, LongFunction, Supplier, and UnaryOperator. These are invoked after SetReturnValue to allow immediate disposal of JVM-backed return objects (defaults to null, invoked via null-conditional).
- Updates MASES.JNet.Specific.Extensions.JavaUtilExtensions to dispose intermediate JVM objects created during collection/map conversions: uses try/finally to Dispose temporary jvmData, keys and values, and adds using for keySet where appropriate to avoid leaking JVM global references.

This prevents leaking JVM global references by releasing them immediately when CLR-side code no longer needs the returned JVM objects.
@masesdevelopers masesdevelopers self-assigned this May 8, 2026
@masesdevelopers masesdevelopers added enhancement New feature or request JNet JNet related issue labels May 8, 2026
@masesdevelopers masesdevelopers added the .NET Pull requests that update .net code label May 8, 2026
masesdevelopers and others added 2 commits May 8, 2026 02:52
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
tInstance.Add(func != null ? func(item) : TJVMTypeInner.ToJVM(item));
var jvmData = func != null ? func(item) : TJVMTypeInner.ToJVM(item);
#endif
using (var disposable = jvmData as IDisposable)
tInstance.Put(keyConverter(item.Key), valueConverter(item.Value));
var key = keyConverter(item.Key);
var value = valueConverter(item.Value);
using (var keyDisposable = key as IDisposable)
var key = keyConverter(item.Key);
var value = valueConverter(item.Value);
using (var keyDisposable = key as IDisposable)
using (var valueDisposable = value as IDisposable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request JNet JNet related issue .NET Pull requests that update .net code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

All developed Java.Util.Function.* classes does not contains the dispose delegate

1 participant