create_input_stream leaks the buffer global ref when malloc fails
create_input_stream promotes the read buffer to a JNI global ref, then returns NULL on malloc failure without DeleteGlobalRef. The caller never receives an InputStream, so it cannot clean that ref up.
Where
app/src/main/cpp/jni/stream/input_stream.c (edcbd95)
- Line 43:
NewByteArraycreates the buffer - Line 44:
NewGlobalRefstores it back intobuffer - Lines 51–53:
mallocfails and the function returns NULL - Lines 67–68:
destroy_input_streamdoes callDeleteGlobalRefonisandbuffer, but only when anInputStreamalready exists
What leaks
On the malloc failure path the global ref in buffer is dropped on the floor. The local ref from NewByteArray is released when the JNI call returns; the global ref is not. destroy_input_stream cannot run, because the only pointer that holds buffer was never returned.
Impact
This is the out-of-memory path for a small InputStream allocation, so it is rare. When it does happen, the global ref and the byte array it keeps alive stay in the global reference table for the rest of the process.
Suggested fix
Call DeleteGlobalRef(buffer) before return NULL in the malloc failure branch. Alternatively, call NewGlobalRef only after malloc succeeds.
Source: xiaojieonly/Ehviewer_CN_SXJ