use of __weak instead of __unsafe_unretained

14 views
Skip to first unread message

Jindrich Sarson

unread,
Feb 12, 2021, 5:31:40 AMFeb 12
to j2objc-discuss
Hi,

there is issue https://github.com/google/j2objc/issues/1356 regarding change of conversion @Weak annotated fields to __weak attribute. It looks like this should be relative safe and easy change. Is there any reason it is not implemented yet? We were changed this in our j2objc build, but I'd like to ask - should I worry about any side effects?

Thanks,

Jindrich

Tom Ball

unread,
Feb 12, 2021, 1:54:27 PMFeb 12
to j2objc-discuss
There shouldn't be side-effects, unless you want your weak references to crash when they're null instead of throwing NullPointerExceptions. :-)

It turns out this was fixed four months ago, just not closed. If you are building j2objc-generated code with ARC, then nothing needs to be done. If you don't build with ARC, compiling with the "-fobjc-weak" flag enables it. The good news is that I added "-fobjc-weak" to the flags Google apps that use non-ARC j2objc, and after four months there still aren't any reported issues.

Do you think we should require this compiler flag for non-ARC code? It seems much safer, with similar behavior as Java's WeakReference class. To require it, j2objc would have to generate the following for sources that have @Weak fields:

#if ! __has_feature((objc_arc_weak)
#error "@Weak annotations in non-ARC code require weak attribute support (-fobjc-weak)"
#endif

I'd also update j2objcc.sh and the docs to add that required flag.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/j2objc-discuss/f4eaa823-01df-4569-ad15-75e5714112c2n%40googlegroups.com.

Jindrich Sarson

unread,
Feb 12, 2021, 3:19:46 PMFeb 12
to j2objc-discuss
Thanks! I downloaded new sources from git and it works perfectly!

I think that -fobjc-weak should be required, because I don't see any reason to use __unsafe_unretained unless you target iOS 4 :-).

Jindrich



Tom Ball

unread,
Feb 23, 2021, 7:17:08 PMFeb 23
to j2objc-discuss
Implemented last week in da2f6cd. To avoid clutter, the check only appears in .m files from Java sources that have a Weak or WeakOuter annotation:

#if !__has_feature(objc_arc_weak)
#error "java/lang/Thread must be compiled with weak references support (-fobjc-weak)"
#endif

Reply all
Reply to author
Forward
0 new messages