public abstract class AbstractCloseableNativeProxy extends AbstractNativeProxy implements CloseableNativeProxy
A native proxy references the corresponding native object by its implementation-specific handle. If handle is zero, the proxy is considered invalid and will not permit referencing any native object. It is perfectly possible to get an invalid proxy (e.g., if a native method fails to allocate a native object).
You must close a native proxy when it is no longer needed to release any resources it holds (e.g., destroy a native object). You may use a try-with-resources statement to do that in orderly fashion. When a proxy is closed, it becomes invalid.
nativeHandle
Modifier | Constructor and Description |
---|---|
protected |
AbstractCloseableNativeProxy(long nativeHandle,
boolean dispose)
Creates a native proxy.
|
protected |
AbstractCloseableNativeProxy(long nativeHandle,
boolean dispose,
AbstractCloseableNativeProxy referenced)
Creates a native proxy.
|
protected |
AbstractCloseableNativeProxy(long nativeHandle,
boolean dispose,
java.util.Collection<AbstractCloseableNativeProxy> referenced)
Creates a native proxy.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the native proxy and releases any native resources associated with this proxy.
|
protected abstract void |
disposeInternal()
Releases any resources owned by this proxy (e.g., the corresponding native object).
|
protected long |
getNativeHandle()
Returns a native implementation-specific handle if it may be safely used
to access the native object.
|
isValidHandle
protected AbstractCloseableNativeProxy(long nativeHandle, boolean dispose)
nativeHandle
- an implementation-specific reference to a native objectdispose
- true if this proxy is responsible to release any resources
by calling disposeInternal()
; false — otherwiseprotected AbstractCloseableNativeProxy(long nativeHandle, boolean dispose, AbstractCloseableNativeProxy referenced)
nativeHandle
- an implementation-specific reference to a native objectdispose
- true if this proxy is responsible to release any resources
by calling disposeInternal()
; false — otherwisereferenced
- a referenced native object, that must be alive
during the operation of this native proxyprotected AbstractCloseableNativeProxy(long nativeHandle, boolean dispose, java.util.Collection<AbstractCloseableNativeProxy> referenced)
nativeHandle
- an implementation-specific reference to a native objectdispose
- true if this proxy is responsible to release any resources
by calling disposeInternal()
; false — otherwisereferenced
- a collection of referenced native objects, that must be alive
during the operation of this native proxyprotected final long getNativeHandle()
The returned value shall only be passed as an argument to native methods.
Warning: do not cache the return value, as you won't be able to catch use-after-free.
getNativeHandle
in class AbstractNativeProxy
java.lang.IllegalStateException
- if this native proxy or any directly or transitively referenced
proxies are invalid (closed or nullptr).public final void close()
CloseableNativeProxy
Notifies the native code that the native object is no longer needed, and may be safely destroyed. Once closed, the proxy becomes invalid.
The implementations must be idempotent — do nothing on consecutive invocations.
close
in interface CloseableNativeProxy
close
in interface java.lang.AutoCloseable
protected abstract void disposeInternal()
This method is only called once from close()
for a valid
proxy and shall not be called directly.
Copyright © 2018 Exonum. All rights reserved.