ndk/media/image_reader: Don't assume ownership of NativeWindow#418
Merged
ndk/media/image_reader: Don't assume ownership of NativeWindow#418
NativeWindow#418Conversation
When implementing proper ownership `acquire()` and `release()` semantics for `NativeWindow` in #207, I thought I had checked all existing calls to `NativeWindow::from_ptr()` to make sure that they return a pointer that we get ownership over, and have to clean up ourselves. This turns out to [not be the case for `AImageReader_getWindow()`]: The ANativeWindow is managed by this image reader. Do NOT call ANativeWindow_release on it. Instead, use AImageReader_delete. And can be trivially reproduced by creating an `ImageReader` and calling `.get_window()`. Dropping the `NativeWindow` is fine but subsequently dropping the `ImageReader` results in a NULL-pointer SEGFAULT. For now calling `clone_from_ptr()` is enough to first acquire an extra reference on the pointer so that ownership remains balanced, but in the future we'd like to investigate a [new non-owned `NativeWindow` type similar to `HardwareBuffer`]. As of writing the following calls to `from_ptr()` remain, that are all confirmed to transfer ownership and require cleanup via `_release()`: - `ASurfaceTexture_acquireANativeWindow()`; - `AMediaCodec_createInputSurface()`; - `AMediaCodec_createPersistentInputSurface()`; - `ANativeWindow_fromSurface()`. [not be the case for `AImageReader_getWindow()`]: https://developer.android.com/ndk/reference/group/media#aimagereader_getwindow [new non-owned `NativeWindow` type similar to `HardwareBuffer`]: #309
rib
approved these changes
Aug 15, 2023
Member
rib
left a comment
There was a problem hiding this comment.
Was initially unsure how it considers the possibility of AImageReader_getWindow failing and not setting a window pointer, but looks like construct_never_null handles that ok.
looks good to me.
Member
Author
|
Yup, should be enough asserts there. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When implementing proper ownership
acquire()andrelease()semantics forNativeWindowin #207, I thought I had checked all existing calls toNativeWindow::from_ptr()to make sure that they return a pointer that we get ownership over, and have to clean up ourselves. This turns out to not be the case forAImageReader_getWindow():And can be trivially reproduced by creating an
ImageReaderand calling.get_window(). Dropping theNativeWindowis fine but subsequently dropping theImageReaderresults in a NULL-pointer SEGFAULT.For now calling
clone_from_ptr()is enough to first acquire an extra reference on the pointer so that ownership remains balanced, but in the future we'd like to investigate a new non-ownedNativeWindowtype similar toHardwareBuffer.As of writing the following calls to
from_ptr()remain, that are all confirmed to transfer ownership and require cleanup via_release():ASurfaceTexture_acquireANativeWindow();AMediaCodec_createInputSurface();AMediaCodec_createPersistentInputSurface();ANativeWindow_fromSurface().