3 ms·
I have encountered very few of these, with Swift. The reference counter seems good. I tend to declare many of my object references as weak, anyway; even if not
by ChrisMarshallNY 4y ago
I have encountered very few of these, with Swift. The reference counter seems good.
I tend to declare many of my object references as weak, anyway; even if not necessary.
Of course, I'm old-fashioned (and I primarily work with UIKit), so I use classes a lot.
If I am doing it the "current" way, most of the program uses value semantics, so it should not be an issue (in theory, anyway).
- anonymouse008 4y agoAye - same here. Was once just a novice who had no idea how an application could hog up 62Gb of ram - and have weird changes to objects that shouldn't. Then I learned about references, and what a biological messaging system really means (thanks, Alan and NotificationCenter!).
- saagarjha 4y ago> I tend to declare many of my object references as weak, anyway; even if not necessary. Why?
- ChrisMarshallNY 4y agoBecause they don't need to be strong. I like to establish basic Quality habits. This is one. I declare object references as weak (or, sometimes -quite rarely, unowned) by default. If they need to be strong, either the compiler will let me know, when I try assigning to it, or the runtime will let me know with a bad access crash. Much quicker debugging than chasing reference loops. Crashes are actually good. I find the cause fairly quickly. I usually don't declare local scope variables weak; only ones that escape the scope. I have found that I seldom need to switch to strong. I also do other "useless" things, like setting the constant before the variable, in comparisons: "Good Grief" == whatCharlieSays I also tend to "in-name" incoming arguments, like so: func compare(thisValue inThisValue: Int, toThisValue inThatValue: Int) -> Int I also write @IBOutlets as explicit optionals, like so: @IBOutlet weak var someLabel: UILabel? as opposed to the default: @IBOutlet weak var someLabel: UILabel! This forces me to unwrap everywhere. I avoid implicit optionals like the plague. I use a lot of guards. I will unwrap implicit optionals in system-provided resources, even though it isn't necessary. I don't really care whether or not they are "necessary," or what others think of me. Every little bit helps. I'm very conscientious about the code I write. I write it very quickly, too. Once something becomes habit, it moves rapidly. "We are what we repeatedly do. Excellence then, is not an act, but a habit" - Incorrectly attributed to Aristotle
- pixel_tracing 4y agoNot sure if declaring weak objects all the time is a good practice, you can introduce silent bugs due to an object deallocating unintentionally in scope. Apple actually recommends using strong with proper architecture and relationships between objects. See https://developer.apple.com/wwdc21/10216 https://developer.apple.com/wwdc21/10216
- ChrisMarshallNY 4y agoReference loops are also “silent” bugs. They are the worst kind of silent, because they usually stay hidden until long after the bug was introduced. My way forces me to deal with the bug, very close to causing it. The bugs aren’t really silent. If the code doesn’t get executed (because of all my guards and whatnot), it’s still a bug. It becomes apparent, quite quickly, and is easier to find. I just put a breakpoint in the unexecuted code, and watch it not happen. Lot better than trying to read tea leaves with Instruments, a month after causing the loop. The issue still happens, but it does so, in a way that is predictable. It’s like deliberately filing weak points into a support strut, so that breaks happen in a known place. The problem isn’t the break, but the orthogonal shear that happens at that point, and the weak point simply forces the break to happen in a place that it can be contained. It almost never happens, but when it does, it saves me a great deal of time. That’s the old McConnell thing about finding bugs near the point of introduction.
- saagarjha 4y ago> Crashes are actually good. I find the cause fairly quickly. I agree, but I can't really square that with the rest of your comment. When you declare everything as weak, things don't crash, you just lose objects and they turn into nil out from under you. Like, the reason your IBOutlets can be weak is your view controller holds onto its view, and then that either directly or indirectly references the thing you actually care about. But if you actually care about it, you should also take responsibility for its lifetime, no? Why rely on some implicit chain to keep the object alive? If you're not careful, you might end up silently dropping the object if you temporarily remove it from the view for layout reasons. It seems very much the opposite of the part where you seem to like manually unwrapping things rather than letting this happen implicitly (which I FWIW don't really agree with, but I don't really want to get into unless you want to hear it :P). I should also note that using weak references comes with a performance penalty over strong ones, but that's a tangential issue.