Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] tag()/setTag() in the SWT-Cocoa implementation

Hi,

Recently I met a weird problem with Display.getFocusControl() method: it was failing with NPE on "OS.JNIGetObject(tag);" call (line 1083). At first glance, this call doesn't do anything that can throw NPE.

After some investigations I've found the reason — tag wasn't an JNI object reference. It was a natively set tag by Cocoa itself. I was using NSAlert class which uses tags for the content view. So when my application was asking for a focus control, SWT was encountering NSAlert's window, asking for a tag in order to restore SWT's class and then failing.

Regarding concretely this issue, the solution is simple: the OS.JNIGetObject(tag); line can be surrounded by a try..catch block.

However, I think that problem has more deep roots. Really, why does SWT use tag()/setTag() methods for storing JNI refs? Since Cocoa can use tag field itself, it doesn't look as a secure solution. There are methods for creating/reading/updating instance variables in Cocoa object. So it would be possible to use some variable like "swt_jni_ref" to store refs.

Please share your thoughts on this.


--
Kalugin Mikhail






Back to the top