Skip to content

Add a JavaVM.CreateValue() method. #3

Closed
@jonpryor

Description

@jonpryor

Context: Xamarin.Android Bug 25443

The problem is that it is often desirable to dispose of handles ASAP because you "know" that the instances won't be used anymore:

using (var typeface = Typeface.Create (...)) {
    // use typeface
}

The problem is that the above code is making an assumption that Typeface.Create() will be returning a new, unique, instance. If it doesn't (as is actually the case), then the returned wrapper could be shared with other threads, and if the handle is Dispose()d while another thread is using it...Bad Things™ happen.

Addressing this involves two parts; see Issue #4 for the second half.

The first half involves adding another "primitive" operation: in addition to JavaVM.GetValue() (Issue #2), we should have JavaVM.CreateValue() methods which always create a new, possibly aliasing, wrapper instance (if a wrapper would be used, e.g. vm.CreateValue<int>(...) clearly doesn't involve a wrapper...).

We would thus have two ways to turn a JNI handle into a value:

  • JavaVM.GetValue() to maintain object identity semantics, maintaining a single wrapper instance per encountered Java instance.
  • JavaVM.CreateValue(), which always creates a new wrapper (when required), allowing the caller to always Dispose() of it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions