The Source for Java Technology Collaboration


-- Main.robertmacgregor - 10 Jun 2005

Sometimes you may see this error message from your Cocoa-Java program:

ObjCJava FATAL:
jobjc_lookupObjCObject(): returning garbage collected java ref for objc object of class TextFieldDelegate
ObjCJava Exit

(This example message uses

TextFieldDelegate
. You'll see a class-name from your program)

Here's Apple's description of the object reference problem with a solution:

http://developer.apple.com/documentation/Cocoa/Conceptual/LanguageIntegration/Concepts/memory.html

A cryptic message but not unknown! There is an issue between Java and Cocoa about memory management.

Java uses garbage collection to reclaim unused memory. Java's memory usage model frees programmers from worrying about allocating and deallocating memory (I realize you know this but I need to make a point about Cocoa). Cocoa is a framework/environment from over 10 years ago. Cocoa doesn't have the benefit of Java's garbage collection and uses a method of reference counting for tracking object use. cocoa programmer's are responsible for incrementing and decrementing the reference count on every object they use. This may sound hard but it's pretty easy in practice. The advantage is Cocoa applications have a very controllable memory footprint.

How does this effect your code? The error message you have is a warning from the bridge between Java and Cocoa that an object on the Cocoa-side attempted to reference a Java object that has been garbage collected. So what's happened is a TextFieldDelegate was garbage collected by Java and OS X's Cocoa environment tried to use it.

Is there a solution? Yes! Typically all you need to do is keep a reference to your object. When Java detects there is a reference present your object will not be garbage collected and Cocoa can continue to use your object. Searching Google for the error message produces some useful techniques for retaining object references, here's two:

http://www.wrenbeck.com/blog.nsf/Archive/EWRK-5RVV5M!OpenDocument

http://www.cocoabuilder.com/archive/message/cocoa/2002/5/12/69419

Topic Jobjc_lookupObjCObject . { Edit | Ref-By | Printable | Diffs r1 | More }
 XML java.net RSS

  

Revision r1 - 2005-06-10 - 20:49:00 - robertmacgregor
Parents: WebHome > MacJavaProjects > MacJavaFaq