This workshop will be retired on May 31, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Ancient History - Pre-ARC8:32 with Gabe Nadel
In days of old, developers coded by candlelight and called "retain" and "release" explicitly. Let's take a trip down memory lane so you'll know how to handle memory management when you're asked to work with some particularly dusty code.
So, let's spend a few minutes talking about the pre-ARC days. 0:00 We're gonna do that by poking around an old sample Apple project called 0:03 RosyWriter. 0:07 Now, RosyWriter was a project Apple gave to developers to help illustrate 0:07 best practices for image capture and processing, but 0:12 we'll just look at how the memory management is done. 0:15 So, don't worry about understanding all the code. 0:17 I'll highlight the relevant parts. 0:19 If you feel like downloading the sample, I've put a link below. 0:21 So, we'll head into RosyWriter.m, 0:25 and we see right up at the top something sticking out. 0:27 We see this keyword, retain. 0:32 Now, many newer iOS developers probably have never seen the word retain in here. 0:35 Well, don't worry, retain in non-ARC code is the same as saying strong with ARC. 0:40 It says, once this memory is allocated retain it, 0:45 until there are no more references to it. 0:49 By the way, we're gonna go over what strong, weak, and 0:51 other common memory directives mean in the next video. 0:54 If we look down here on line 67, we see that the preview view, 0:57 which is of type OpenGLPixelBufferView, and no, don’t worry, 1:02 you don’t have to know what that means, is declared with retain. 1:06 So where do we first retain that memory? 1:11 Well, if we were to head down to about line 214, if my notes are correct. 1:14 We're gonna see that we allocate the memory right here with alloc self.preview. 1:21 We're gonna alloc that class. 1:29 You may also notice that we have an autorelease call here. 1:32 Autorelease, like retain, 1:35 is an easy way to tell that you're dealing with pre-ARC code. 1:37 What auto release does is indicate we are allocating memory now and 1:40 we need it for now, but when we complete this method and 1:44 the run loop ends, you can decrement the retain count. 1:47 And if that leaves the count at zero, the memory can be released. 1:51 I'm saying autorelease now, so I don't have to explicitly call release later. 1:54 If you wanna make sure the object isn't released completely, you'd better call 1:59 retain somewhere else in the block to make sure that the count stays above zero. 2:03 Speaking of deallocating, or dealloc, 2:07 if we head up to the top of our file here, we're gonna see a call to super dealloc, 2:10 and that's gonna be inside our dealloc method. 2:16 Now, this code up here just gets rid of some of the notifications that we set up. 2:19 That's no big deal. 2:24 That's standard cleanup stuff that we would do in an ARC project as well. 2:25 But down here, we have some explicit calls to release all of the properties 2:29 that we declared up top, that's up here, as retain. 2:33 Now we're customizing our dealloc method here, but we wouldn't actually ever 2:39 call this method explicitly, rather it would be invoked by runtime automatically. 2:43 When that happens, 2:48 all of our properties will be released if their retained counts are still positive. 2:49 So, let's say you have this delightful project, or any non-ARC project, or class, 2:53 or file, and it's dropped in your lap. 2:57 What do you do? 2:59 Well, in all likelihood, 3:01 you don't wanna continue working with it as it is as non-ARC, but 3:02 rather, implement ARC and bring it up to speed with the present day practices. 3:05 To do this, you could go line by line through the entire project, but only do 3:09 that if you have a very wealthy, oblivious client who is paying you by the hour. 3:13 In all other cases, you'll wanna use the handy dandy tool provided in Xcode. 3:19 Now before you actually do this, 3:24 you'll wanna save a copy of your project somewhere safe, just in case. 3:25 Now, this is a project I just downloaded from Apple, so 3:30 I'm not gonna bother doing that. 3:32 Okay, so to convert, we're gonna head to 3:34 Edit > Convert > To Objective-C ARC. 3:39 And there we go. Now if we expand this view, 3:46 right here, we get to see which files we wanna convert. 3:49 Now, we're gonna convert all of them, but often, you might find it more manageable 3:54 to convert just a few files at a time in small batches. 3:58 This allows you to not get overwhelmed if errors pop up, and 4:01 it also gives you an opportunity to make some changes, test your projects, 4:05 save or commitment the changes, and then repeat. 4:09 The changes we're making shouldn't cause problems but they could, and 4:12 it's nice to know that you have just a small change set that you have to review 4:16 if something goes wrong. 4:19 But again, this isn't so many files, so we'll just do them all at once. 4:20 We can hit check to get that started. 4:24 We hit check and okay, it looks like we have a little manual work to do. 4:29 Let's check out some of these errors and see what's going on. 4:34 Alternatively, we could take the suggestion here to change our preferences 4:37 to continue building, but we're not going to do that here. 4:43 Ok. 4:46 Now, when you do this in your project, you may find different errors, but 4:48 we'll just go through the ones that I found and fix them. 4:52 So let's start here. 4:55 Okay. 4:57 Now for some of these, we have some useful suggestions. 4:59 So let's try those first. 5:02 We're gonna fix this using that bridge. 5:04 And the same thing here. 5:07 Let's move down to some of the others. 5:09 For this case, we can actually get rid of this loadWeak there and 5:12 leave it as delegate = _delegate. 5:17 We're gonna do the same here. 5:23 It looks like we've changed our comment that everything we need. 6:15 Let's try running that again. 6:18 We still found one issue. 6:31 Let's investigate that. 6:32 We can fix that using their suggestion. 6:37 I'll try it one more time. 6:41 Okay, now we're gonna be guided through the steps. 6:50 Okay, now for our little project, this isn't all that impressive, but 6:51 you can see here that we could get all the changes we've made and 6:57 go through them one by one. 7:03 And in a real live project, that's gonna be a lot more useful. 7:06 But here you see that, besides the things we change, 7:09 we're getting retained is gonna become strong. 7:11 We know that that's an ARC thing and that's good. 7:13 And if we went through we'd similar stuff, released being removed, 7:17 dealloc and release being removed, so that's all fine. 7:20 We'll hit save. 7:23 Okay, so it looks like we're done there. 7:25 Let's do a little quick smoke test to make sure that our app still runs. 7:26 Okay, great. Our app still builds. 7:34 In the real world, of course, you'll wanna do some actual testing inside the app, 7:37 just to be sure nothing's gone awry. 7:40 And, by the way, RosyWriter is an old project, and 7:42 it will crash in the simulator if you tap that red button. 7:45 It's meant to be used on a device. 7:48 But that has nothing to with the changes we just made. 7:50 It happens even when you try to download a fresh copy from the Apple developer site. 7:53 The work we just did is something you may encounter soon, or 7:58 may never even have to deal with. 8:01 It really depends on what kind of legacy code you encounter. 8:02 That said, it's an important part of the evolution of iOS and 8:06 something that devs love to talk about in interviews. 8:09 Even if you don't find yourself needing to interact with pre-ARC code, 8:12 it's always a good thing to review before heading out, resume in hand. 8:16 Okay, I think we've spent enough time in the Jurassic age of iOS memory management. 8:20 In the next video, we'll move forward a bit and 8:25 delve into the far more relevant world of Objective-C ARC. 8:27
You need to sign up for Treehouse in order to download course files.Sign up