New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhancement request: optionally allow @WeakOuter to be the default (with new @StrongOuter annotation) #1029
Comments
The preferred pattern (Effective Java #22) is to mark all inner classes as static unless there is an actual need for the inner class to reference the outer class. By marking an inner class static, the compiler (javac or j2objc) won't define an outer instance field, reducing the coupling of the two classes and making the inner class more efficient. Your app should need very few inner classes that are not static. For what it's worth, the only reason for needing @WeakOuter is for those few classes which return inner class instances that may outlive the outer instance. For example, HashMap has inner class collections of keys, values, and entries, and accessors that return those instances. In the whole JRE, there are only 16 classes that use this pattern, and they all need a WeakOuter. If your app is using this pattern widely, consider having those accessors return copies instead. |
I do know the difference between regular inner classes and static ones. :-) We are constructing GUI in J2ObjC, so we have a lot of these.
My additional problem here was that I don't actually want my developers, developing the shared code to remember inserting "@weak" everywhere, cause they won't. Also consider the difficulty of declaring a Lambda as a @weak reference (define a separate variable, etc.). |
It seems like this boils down to whether the default is "safe but possibly leaky" vs "unsafe but less likely to leak". I think j2objc's current default is a good choice, since a pass through the Leaks instrument by someone familiar with ObjC memory semantics is critical for finding leaks regardless of this issue. To me it seems like being able to mark anonymous classes and lambdas as |
What would break if a lambda's outer reference was always __weak? Any thoughts on making Java 8 the minimum OS? |
If I may just offer an opinion on "safe but possibly leaky" vs "unsafe but less likely to leak". I have 5 developers developing that client application and 2 QAs testing it. None of those developers ever developed anything in ObjC or worked with a Mac! Once I changed J2ObjC source to make outers weak by default, I asked one of the QAs to run the app in the debugger and "do the app flow". And he found the needed two strong outers in less than half an hour! Fixing a memory leak is very hard: as you said, you need a serious amount of time, by a good developer, familiar with Apple tools. I'm obviously not saying that Weak should be the default out of the box, but having such an option in a centralised manner, I would say, could be a great option. Thank you! |
I agree, that it would be nice to have this as j2objc translator option. We are in process of cleaning memory leaks, our code is using tons of callbacks and it is really nightmare. |
The request is for a new command line argument for j2objc to optionally consider all outer references "weak". In this mode all outer references are week and a new annotation
@StrongOuter
can be used to make a specific inner class reference outer object strongly.The text was updated successfully, but these errors were encountered: