The Cocoa runtime uses two modes of managing object allocation and de-allocation:

Developers who create libraries that can be used in other applications (like Java) need to be aware and write Objective-C code that works in both modes. When Java's garbage collector is added into the mix with JNI local refs, global refs, and passing native objects up to Java as jlongs, memory management can get quite complicated!

There are a few simple rules to follow when writing JNI code that handles Cocoa objects:

Always use JNF_COCOA_ENTER()/JNF_COCOA_EXIT()

As a rule, Java objects should own native objects in jlongs

Native objects held by Java objects must have a "hard" CF-retain count of 1 before being passed up to Java

Don't manage JNI global refs yourself