Memory 3 - Fixing Memory Leaks in Closures with Capture List (iOS, Xcode 9, Swift 4)

  Переглядів 18,420

Mark Moeykens

Mark Moeykens

6 років тому

🌐 www.bigmountainstudio.com
Swift Memory Mastery Series - Part 3
Don't worry if you don't know what a closure or capture list is. This video will teach you.
You will learn:
* What closures are
* How to pass data into closures
* How closures can obtain values outside of themselves
* How to explicitly pass data into closures
* How to change the strength of the value coming into a closure to weak or unowned
* How to create a memory leak (retain cycle) with closures (very common)
* How to fix closure memory leaks with capture lists
* Understanding memory leaks in closures
FREE SwiftUI PictureBook:
📙 SwiftUI Views: www.bigmountainstudio.com/fre...
COURSES
🎦 This course is hosted on Code with Chris: bigmtn.studio/cwc
SOCIAL
🔷 Twitter: / bigmtnstudio
🟣 Github: github.com/bigmountainstudio
#markmoeykens #bigmountainstudio #iosdevelopment

КОМЕНТАРІ: 111
@krutomjer
@krutomjer 6 років тому
Dude, like, this is mind blowing how you explain it and how logical it is.
@bigmtnstudio
@bigmtnstudio 6 років тому
Thank you, I try to make it as easy as possible. 😀
@hamzaalmass8826
@hamzaalmass8826 6 років тому
Great Video , Thank You Very Much
@MrTalhakhan01
@MrTalhakhan01 6 років тому
Awesome Man. Thanks much
@hoangson9872
@hoangson9872 4 роки тому
Great video. Thanks.
@user-lv9ki3eq1l
@user-lv9ki3eq1l 3 роки тому
Great video! Straightforward, simple, easy to understand. Learned a lot just from this video thanks!
@Princebharti9971
@Princebharti9971 5 років тому
perfect , I became fan of yours teaching style ..awsome !!
@NLBAMF
@NLBAMF 6 років тому
awesome video again .. looking fwd to the next episode. 👏 👏
@bigmtnstudio
@bigmtnstudio 6 років тому
Thanks, Ahmed. I'm almost done with the outline and the slides. Should be recording by tomorrow.
@aceyoung2710
@aceyoung2710 6 років тому
thank you so much ! you make it so easy to learn !
@bigmtnstudio
@bigmtnstudio 6 років тому
You're welcome! I try to teach how I want to learn. And I too like it when others make it easy for me to learn.
@baloiradu7460
@baloiradu7460 4 роки тому
Literally watching the ads to support this type of content
@saikatdas8819
@saikatdas8819 5 років тому
Nice Video sir, it helped me to clear my concepts Thank you, keep growing
@karlapangilinan6829
@karlapangilinan6829 3 роки тому
This is more than 2 yrs old but still better than most, if not all, tutorial videos I encountered about this topic. You're really good at this!
@bigmtnstudio
@bigmtnstudio 3 роки тому
Thank you so much, Karla! 😊
@karlapangilinan6829
@karlapangilinan6829 3 роки тому
@@bigmtnstudio oh, no. Thank YOU for sharing your knowledge and making these concepts easier for us to grasp!
@ahmadissa8607
@ahmadissa8607 6 років тому
The best ever describe memory leaks, Thank you
@bigmtnstudio
@bigmtnstudio 6 років тому
Thank you, Ahmad. I'm glad you liked it. 😃
@thanhnguyenchi5384
@thanhnguyenchi5384 6 років тому
Very clear tutorial, thank you so much, Mark 💙
@bigmtnstudio
@bigmtnstudio 6 років тому
You're welcome, Thanh! Glad you enjoyed it. 😃
@yuriiohiria5559
@yuriiohiria5559 4 роки тому
The content is at the top level! Thank you!
@bigmtnstudio
@bigmtnstudio 4 роки тому
No problem. Yuri!
@nickng8069
@nickng8069 5 років тому
Now I know what weak and unowned really mean! Thanks
@jasonkendall3610
@jasonkendall3610 4 роки тому
This was a great addition to my research relating to memory management!
@bigmtnstudio
@bigmtnstudio 4 роки тому
That's great, Jason. Glad it was helpful!
@agung_laksana
@agung_laksana 6 років тому
hi Mark, Thank you very much for sharing knowledge. I finally UNDERSTAND this topic, you made it really really really easy
@agung_laksana
@agung_laksana 6 років тому
it would be great if you also teach us about concurrency and RxSwift . since for me personally i don't really grasp those topic. Thank you very much Mark :D
@bigmtnstudio
@bigmtnstudio 6 років тому
That's awesome, Agung! This is a tough topic for any developer to grasp. It was especially tough for me to break this knowledge down into simple parts that could be taught in a way that made sense. So I'm really glad you appreciate it and more importantly, understand it. That was my goal for all of you. To present in a way that EVERYONE could understand easily.
@snakery18
@snakery18 6 років тому
Incredibly well-explained, and the video is edited really well. Thank you!
@bigmtnstudio
@bigmtnstudio 6 років тому
You're welcome, Ryan! Glad you liked it!
@silver10188
@silver10188 5 років тому
Best video, I'll buy your course :)
@bigmtnstudio
@bigmtnstudio 5 років тому
Awesome!
@3gzotic
@3gzotic 6 років тому
Thanks Mark, you are THE BEST :)
@bigmtnstudio
@bigmtnstudio 6 років тому
Thank you so much! I'm glad you liked it. 😀
@neyaz8421
@neyaz8421 3 роки тому
Thanks for explaining in such simple details . Very helpful
@bigmtnstudio
@bigmtnstudio 3 роки тому
You're welcome, Neyaz!
@DewTime
@DewTime 6 років тому
Very clear, thanks
@bigmtnstudio
@bigmtnstudio 6 років тому
Oh good, this topic was a challenge for me to make clear and easy to understand.
@leikolaboeproduction5785
@leikolaboeproduction5785 6 років тому
Bro, Truly appreciate the effort that you are putting in to these videos. Great work! Thank you!
@bigmtnstudio
@bigmtnstudio 6 років тому
Thanks! I'm glad you're liking them. 😃
@sami790311
@sami790311 6 років тому
Honestly, the best Swift videos on UKposts are from this guy!
@bigmtnstudio
@bigmtnstudio 6 років тому
Ha ha, thanks man!
@markarjohnsanantonio1615
@markarjohnsanantonio1615 6 років тому
Haha! At 1 minute, that is the same face I make when I get mental block while developing something. Amazing video as always and I have been using closures all this time unaware of the idea that they can cause memory leaks. I'll take note of this, thanks!
@bigmtnstudio
@bigmtnstudio 6 років тому
Ha ha, yes, Mark! I was going to cut that out but decided to leave it in at the last minute to seem more human. Ha ha. Thanks and I'm glad you liked the video!
@rakeshjoshi5909
@rakeshjoshi5909 3 роки тому
Awesome....Mark....I watched all videos got whole concept in just 2 hours
@bigmtnstudio
@bigmtnstudio 3 роки тому
Awesome, Rakesh!
@yndrywjynsyn
@yndrywjynsyn 6 років тому
Great video! Your teaching matches what I learned from the Stanford iOS class on Closures which taught this: [unowned vc = self] in a closure.
@bigmtnstudio
@bigmtnstudio 6 років тому
That's awesome! Right on target. 😃
@sathya1nath653
@sathya1nath653 6 років тому
hi bro , thanks bro you really helped me to retrive memory issues . I really liked your tutorials thanks you bro please upload thease type of videos with latest updates
@bigmtnstudio
@bigmtnstudio 6 років тому
Thank you! I created a series called "Swift Memory Mastery". It is using the latest updates.
@gastongasquet3594
@gastongasquet3594 2 роки тому
It’s amazing how easy you explained things, great content once again. It would be nice a Unit Test video 😃. Thanks again. Subscribed.
@bigmtnstudio
@bigmtnstudio 2 роки тому
Thank you, I'm glad you liked it!
@miguelangeljimenezhuerta1036
@miguelangeljimenezhuerta1036 6 років тому
Great video, thanks for sharing your knowldege, I will let know all my friends about your channel xD
@bigmtnstudio
@bigmtnstudio 6 років тому
Thanks, Miguel!
@vunam0502
@vunam0502 5 років тому
Hi Mark, Great article! Thank you so much! Just want to point out something that you mentioned at 7:20. Outlets always are weak by default because the ViewController holds a strong reference to its containerView. If the outlet is strong, the containerView will hold a strong reference to that outlet. So that we have ViewController and containerView will together hold the outlet, which will make the retain cycle. Hope this helps ^^ (sorry for my bad english)
@bigmtnstudio
@bigmtnstudio 5 років тому
Oh OK. Well thank you for taking the time to share this with me and other developers that watch the video! 👍🏼
@BaPii
@BaPii 4 роки тому
A little late comment here but... Mark, your lessons are crystal clear. I've known and been subscribed to your channel for quite a while now, and everytime something's unclear, I come back to these high quality videos. Once I find myself in a better financial situation (currently bootstrapping my startup) I'll surely support you through Patreon.
@bigmtnstudio
@bigmtnstudio 4 роки тому
Thank you, sir. Glad you like my videos and teaching style!
@vinbhai4u
@vinbhai4u 5 років тому
love the way u were stuck in the intro quite natural :)
@tomaszakwin8409
@tomaszakwin8409 6 років тому
Nice Job with this Memory Series! Could you stay in this topic and show how to fix the problem with Malloc leaks and Zombies object? It would be great!
@bigmtnstudio
@bigmtnstudio 6 років тому
Thanks, Tomasz! I have another video I'm going to do so maybe I can fit this in. Thanks for the suggestion!
@raghunathan3244
@raghunathan3244 5 років тому
Firstly I thanks you for your wonderful video , but I've a doubt ViewController class have Weak reference to its dateLabel property , but in your explanation its stated as Strong reference.
@hosseindastan6221
@hosseindastan6221 6 років тому
Thanks very good
@bigmtnstudio
@bigmtnstudio 6 років тому
Thanks, Hossein!
@zahidshaikh7228
@zahidshaikh7228 6 років тому
Great work sir make video on extension also
@bigmtnstudio
@bigmtnstudio 6 років тому
Thanks, Zahid!
@cinquain0
@cinquain0 5 років тому
Mmmmmmmhhh damn... this video definitely opened my eyes about closures. I probably created ton of retain cycles in the past without even realizing it
@bigmtnstudio
@bigmtnstudio 5 років тому
Ha ha ha. Yeah, I'm sure I have too!
@ramitech.1216
@ramitech.1216 6 років тому
Great tutorial , now I think I will find more than 99 retain cycles in my code :D
@bigmtnstudio
@bigmtnstudio 6 років тому
Rami Tech. Ha ha ha. Oh man, well, now you know how to fix them!
@shrikanttanwade8554
@shrikanttanwade8554 5 років тому
Thank you................
@bigmtnstudio
@bigmtnstudio 5 років тому
You're welcome, sir. ✊️
@shrikanttanwade8554
@shrikanttanwade8554 5 років тому
Please you make video on Swift Properties in deep ?
@MuhammadAli-zv5vz
@MuhammadAli-zv5vz 6 років тому
Awasome @Mark
@bigmtnstudio
@bigmtnstudio 6 років тому
Muhammad Ali, thank you! 😊
@goldfish8196
@goldfish8196 Рік тому
Bravo.
@bigmtnstudio
@bigmtnstudio Рік тому
Thank you!
@nitishkumar-ue7cf
@nitishkumar-ue7cf 4 роки тому
@Mark one question here, what will happen if i create IBoutlet as strong, is this going to create a memory leak
@monicaluo1122
@monicaluo1122 4 роки тому
Thank you very much for the video! I have a question on 8:15. Why can't we just change the popup to be a weak var? Namely weak var popup?
@bigmtnstudio
@bigmtnstudio 4 роки тому
Excellent question, Mengyi! The answer is: You totally can! Yes, when looking at the image after this point, where I show the memory graph, you can break any one of those strong references to fix the retain cycle. In the previous video (ukposts.info/have/v-deo/aXyfnaaPoImjtIE.html) I talked about the 2-Step method of fixing a retain cycle. It's up to the developer to determine who the parent and child classes are. So in this video, I was making the popup the parent and the onSave the child. But you could totally make the ViewController the parent and the popup the child. Both will work. The main thing is you need to create a break somewhere in the retain cycle between the 3 reference types.
@monicaluo1122
@monicaluo1122 4 роки тому
@@bigmtnstudio Thank you very much, Mark! I just wanted to know that there could be another solution.
@stevengao8345
@stevengao8345 6 років тому
thanx Mr. Mark, for this great video, I have two questions after watched this video 2 times: first: will always be a retain cycle if I put an outside variable/property inside the closure ? and second : sometimes I am not very sure I got a retain cycle or not, can I use [weak self ] capture list anyway in all my closure ? just for sure to remove the potential hazard , will it cost problem later on? thanks very much
@bigmtnstudio
@bigmtnstudio 6 років тому
Steven Gao, there will never be a retain cycle of the variable/parameter being captured is a value type (see video #1). If it's a reference type it really depends what the code looks like. You can always use [weak self] in a closure with no noticeable performance difference. There is a tiny overhead but your users will most likely never even notice because it's so fast.
@rushangpatel1814
@rushangpatel1814 6 років тому
Thanks For the Video ... but I suggest to you plz make Video in How to Use OOPS Concept in RealTime in Swift or while developing Application..That is more important for everyone.
@bigmtnstudio
@bigmtnstudio 6 років тому
Thanks, Rushang! I'll make note of your request. Thank you.
@bhushanpawarscreativity3645
@bhushanpawarscreativity3645 5 років тому
Great Tutorial @Mark, Thanks a ton. Apple must hire you to modify their documentation to make it simpler to understand.
@bigmtnstudio
@bigmtnstudio 5 років тому
No kidding! If their documentation was open source, I'd definitely be contributing. 😃
@bhushanpawarscreativity3645
@bhushanpawarscreativity3645 5 років тому
@@bigmtnstudio BTW, your videos are so good and I am learning a lot from it and guess what Promoting :D Keep posting the good content.
@lukaszstocki6998
@lukaszstocki6998 5 років тому
Forwarder might be something along "responder chain". But as for "partial apply" is a concept connected with curring. For example you have block that takes two ints as arguments but at the moment you have only one value available. So what you can do is to create a block that will take this one argument and returns another block that expects the second. Because all variables are captured in the most inner scope then there you can complete your computation :) In this context I'm guessing that the "hidden" argument of "self" is passed in :) You can check this playground: iswift.org/playground?uonej7&v=4
@abrahamgonzalez1721
@abrahamgonzalez1721 4 роки тому
add lifetimetracker library for dev builds and you will see all the leaks that closure strong references create
@ammarmujib
@ammarmujib 5 років тому
according to docs retain cycle is only created if a class property refer to a closure (ref: docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html ). And in your example you showed UIView.animate() closure which i think wouldnt create retain cycle. If i m wrong please clear me.
@GuerraDeAjedrez
@GuerraDeAjedrez 5 років тому
hi. so doing [weak label = self.myLabel! ] and the rest. the reference count is 1. in this example it is not necessary o possible to set label or vc to nil . i guesS xD . . great tutorials . i like to learn theory. i helps m understand why im doing what im doing :D
@MuhammadAli-zv5vz
@MuhammadAli-zv5vz 6 років тому
Mark I have a issue on my project and donot understand the error can you tell me this: LOG SocketIOClient{/swift}: Handling event: error with data: ["Tried emitting newMessage when not connected"]
@testuser10
@testuser10 6 років тому
Wow! Just wow! If i get a chance to visit US i will definitely visit you sir! Is "MARK: - " in swift is your name ?? Hahaha:) Now if i do work with closure it will definitely reminds me of you! and you'd be like no bruh not this way! Haha . I still have to watch this series on repeat mode. Thank you for this amazing knowledge.
@bigmtnstudio
@bigmtnstudio 6 років тому
Ha ha ha, thanks, Kishan!
@akramyounes4298
@akramyounes4298 6 років тому
Hi, mark, i have a question about if we put (popup.onSave = nill), what will happen to the clouser, does it remains in memory?
@bigmtnstudio
@bigmtnstudio 6 років тому
No it does not! That is a valid way to remove it.
@marcelagoncalvesteixeirade3674
@marcelagoncalvesteixeirade3674 6 років тому
Good good !!!!👏👏👏👏😃😃😃
@bigmtnstudio
@bigmtnstudio 6 років тому
Obrigado, Marcela!
@marcelagoncalvesteixeirade3674
@marcelagoncalvesteixeirade3674 6 років тому
Mark Moeykens thanks😃
@JacksonSmith
@JacksonSmith 4 роки тому
u great 🙈🙊🙉.
@bigmtnstudio
@bigmtnstudio 4 роки тому
Thanks, Jackson! Glad you liked the video. 😃 Did you do the whole series or just this one?
@AnuragSharma-wb1fc
@AnuragSharma-wb1fc 5 років тому
HI Mark, I tried to find leaks through instrument for this example but instrument unable to detect leaks. Thanks and Regards Anurag Sharma
@bigmtnstudio
@bigmtnstudio 5 років тому
Hi Anurag. Intruments can be weird sometimes as different versions of Xcode and Simulators come out. I have noticed the same thing. One time I run the app, no leaks. I make NO changes. I run it again, it finds a leak. I really hate this kind of inconsistency with Apple's Instrument tools. I wish they were better too. ☹️
@AnuragSharma-wb1fc
@AnuragSharma-wb1fc 5 років тому
thanks mark , your videos are mind blowing . Its really helpful.
@TArsVEVO
@TArsVEVO 5 років тому
I think UIView animation doesn't make memory leak because the ViewController doesn't reference to it
@bigmtnstudio
@bigmtnstudio 5 років тому
Yeah, that's a good point. There's no circular reference, right? And I've done tests with this too and found that a view controller will still get released from memory even if you don't use [weak self] when doing animations.
@sitecocu
@sitecocu 3 роки тому
@@bigmtnstudio please fix your video too. Also non-escaping closure that doesn't delay its execution can't create a retain cycle.
@tenminutetokyo2643
@tenminutetokyo2643 6 років тому
DOOD!
@bigmtnstudio
@bigmtnstudio 6 років тому
Hiya, 10! 😄
Memory 1 - Value Types vs Reference Types (iOS, Xcode 9, Swift 4)
11:57
Mark Moeykens
Переглядів 26 тис.
КТО СМОГ ПОБЕДИТЬ?😳
00:36
МЯТНАЯ ФАНТА
Переглядів 602 тис.
когда одна дома // EVA mash
00:51
EVA mash
Переглядів 8 млн
Memory 2 - Finding and Fixing Memory Leaks (iOS, Xcode 9, Swift 4)
34:31
Mark Moeykens
Переглядів 49 тис.
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Переглядів 55 тис.
How to use the Coordinator pattern in iOS
18:12
Paul Hudson
Переглядів 70 тис.
Retain Cycles: How to Detect with Instruments Profiler!
13:55
Lets Build That App
Переглядів 47 тис.
How Do You Write Closures in Swift?
15:55
Lets Build That App
Переглядів 112 тис.