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

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 พ.ย. 2024

ความคิดเห็น • 112

  • @krutomjer
    @krutomjer 6 ปีที่แล้ว +27

    Dude, like, this is mind blowing how you explain it and how logical it is.

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว +1

      Thank you, I try to make it as easy as possible. 😀

  • @neyaz8421
    @neyaz8421 4 ปีที่แล้ว +1

    Thanks for explaining in such simple details .
    Very helpful

  • @karlalalalalala
    @karlalalalalala 4 ปีที่แล้ว +1

    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  4 ปีที่แล้ว

      Thank you so much, Karla! 😊

    • @karlalalalalala
      @karlalalalalala 4 ปีที่แล้ว

      @@bigmtnstudio oh, no. Thank YOU for sharing your knowledge and making these concepts easier for us to grasp!

  • @tarikios
    @tarikios 4 หลายเดือนก่อน

    You are by far the best channel producing content about iOS. I wish you would continue to share such an amaziiing videos

  • @sami790311
    @sami790311 6 ปีที่แล้ว +1

    Honestly, the best Swift videos on TH-cam are from this guy!

  • @vunam0502
    @vunam0502 6 ปีที่แล้ว +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  6 ปีที่แล้ว

      Oh OK. Well thank you for taking the time to share this with me and other developers that watch the video! 👍🏼

  • @BaPii
    @BaPii 5 ปีที่แล้ว +1

    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  5 ปีที่แล้ว +1

      Thank you, sir. Glad you like my videos and teaching style!

  • @rakeshjoshi5909
    @rakeshjoshi5909 3 ปีที่แล้ว +1

    Awesome....Mark....I watched all videos got whole concept in just 2 hours

  • @jasonkendall3610
    @jasonkendall3610 5 ปีที่แล้ว +2

    This was a great addition to my research relating to memory management!

    • @bigmtnstudio
      @bigmtnstudio  5 ปีที่แล้ว

      That's great, Jason. Glad it was helpful!

  • @누텔라-x9r
    @누텔라-x9r 3 ปีที่แล้ว

    Great video! Straightforward, simple, easy to understand. Learned a lot just from this video thanks!

  • @baloiradu7460
    @baloiradu7460 4 ปีที่แล้ว

    Literally watching the ads to support this type of content

  • @yndrywjynsyn
    @yndrywjynsyn 6 ปีที่แล้ว +2

    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. 😃

  • @cinquain0
    @cinquain0 6 ปีที่แล้ว +3

    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  6 ปีที่แล้ว

      Ha ha ha. Yeah, I'm sure I have too!

  • @snakery18
    @snakery18 6 ปีที่แล้ว +1

    Incredibly well-explained, and the video is edited really well. Thank you!

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว

      You're welcome, Ryan! Glad you liked it!

  • @nickng8069
    @nickng8069 6 ปีที่แล้ว

    Now I know what weak and unowned really mean! Thanks

  • @Princebharti9971
    @Princebharti9971 6 ปีที่แล้ว

    perfect , I became fan of yours teaching style ..awsome !!

  • @markarjohnsanantonio1615
    @markarjohnsanantonio1615 6 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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!

  • @aceyoung2710
    @aceyoung2710 6 ปีที่แล้ว +1

    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.

  • @gastongasquet3594
    @gastongasquet3594 3 ปีที่แล้ว +1

    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  3 ปีที่แล้ว

      Thank you, I'm glad you liked it!

  • @saikatdas8819
    @saikatdas8819 6 ปีที่แล้ว

    Nice Video sir, it helped me to clear my concepts Thank you, keep growing

  • @agung_laksana
    @agung_laksana 6 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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.

  • @leikolaboeproduction5785
    @leikolaboeproduction5785 6 ปีที่แล้ว +1

    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. 😃

  • @silver10188
    @silver10188 5 ปีที่แล้ว +1

    Best video, I'll buy your course :)

  • @thanhnguyenchi5384
    @thanhnguyenchi5384 6 ปีที่แล้ว +1

    Very clear tutorial, thank you so much, Mark 💙

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว

      You're welcome, Thanh! Glad you enjoyed it. 😃

  • @ahmadissa8607
    @ahmadissa8607 6 ปีที่แล้ว +1

    The best ever describe memory leaks, Thank you

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว

      Thank you, Ahmad. I'm glad you liked it. 😃

  • @NLBAMF
    @NLBAMF 6 ปีที่แล้ว +1

    awesome video again .. looking fwd to the next episode. 👏 👏

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว +1

      Thanks, Ahmed. I'm almost done with the outline and the slides. Should be recording by tomorrow.

  • @yuriiohiria5559
    @yuriiohiria5559 4 ปีที่แล้ว

    The content is at the top level! Thank you!

  • @ramitech.1216
    @ramitech.1216 6 ปีที่แล้ว +1

    Great tutorial , now I think I will find more than 99 retain cycles in my code :D

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว +1

      Rami Tech. Ha ha ha. Oh man, well, now you know how to fix them!

  • @3gzotic
    @3gzotic 6 ปีที่แล้ว +2

    Thanks Mark, you are THE BEST :)

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว +1

      Thank you so much! I'm glad you liked it. 😀

  • @tomaszakwin8409
    @tomaszakwin8409 6 ปีที่แล้ว +1

    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!

  • @hamzaalmass8826
    @hamzaalmass8826 6 ปีที่แล้ว +1

    Great Video , Thank You Very Much

  • @vinbhai4u
    @vinbhai4u 6 ปีที่แล้ว

    love the way u were stuck in the intro quite natural :)

  • @hoangson9872
    @hoangson9872 5 ปีที่แล้ว

    Great video. Thanks.

  • @sathya1nath653
    @sathya1nath653 6 ปีที่แล้ว +1

    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.

  • @DewTime
    @DewTime 6 ปีที่แล้ว +2

    Very clear, thanks

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว

      Oh good, this topic was a challenge for me to make clear and easy to understand.

  • @goldfish8196
    @goldfish8196 2 ปีที่แล้ว +1

    Bravo.

  • @MrTalhakhan01
    @MrTalhakhan01 6 ปีที่แล้ว

    Awesome Man. Thanks much

  • @bhushanpawarscreativity3645
    @bhushanpawarscreativity3645 5 ปีที่แล้ว +1

    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.

  • @rushangpatel1814
    @rushangpatel1814 6 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      Thanks, Rushang! I'll make note of your request. Thank you.

  • @monicaluo1122
    @monicaluo1122 4 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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 (th-cam.com/video/1LnipXiSrSM/w-d-xo.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 ปีที่แล้ว +1

      @@bigmtnstudio Thank you very much, Mark! I just wanted to know that there could be another solution.

  • @zahidshaikh7228
    @zahidshaikh7228 6 ปีที่แล้ว +1

    Great work sir make video on extension also

  • @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.

  • @miguelangeljimenezhuerta1036
    @miguelangeljimenezhuerta1036 6 ปีที่แล้ว +1

    Great video, thanks for sharing your knowldege, I will let know all my friends about your channel xD

  • @stevengao8345
    @stevengao8345 6 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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.

  • @shrikanttanwade8554
    @shrikanttanwade8554 6 ปีที่แล้ว +3

    Thank you................

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว

      You're welcome, sir. ✊️

    • @shrikanttanwade8554
      @shrikanttanwade8554 6 ปีที่แล้ว

      Please you make video on Swift Properties in deep ?

  • @hosseindastan6221
    @hosseindastan6221 6 ปีที่แล้ว +1

    Thanks very good

  • @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

  • @TArsVEVO
    @TArsVEVO 6 ปีที่แล้ว +6

    I think UIView animation doesn't make memory leak because the ViewController doesn't reference to it

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว +4

      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.

  • @MuhammadAli-zv5vz
    @MuhammadAli-zv5vz 6 ปีที่แล้ว +1

    Awasome @Mark

    • @bigmtnstudio
      @bigmtnstudio  6 ปีที่แล้ว

      Muhammad Ali, thank you! 😊

  • @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"]

  • @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

  • @ammarmujib
    @ammarmujib 6 ปีที่แล้ว

    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

  • @abrahamgonzalez1721
    @abrahamgonzalez1721 4 ปีที่แล้ว

    add lifetimetracker library for dev builds and you will see all the leaks that closure strong references create

  • @JacksonSmith
    @JacksonSmith 5 ปีที่แล้ว +1

    u great 🙈🙊🙉.

    • @bigmtnstudio
      @bigmtnstudio  5 ปีที่แล้ว

      Thanks, Jackson! Glad you liked the video. 😃 Did you do the whole series or just this one?

  • @akramyounes4298
    @akramyounes4298 6 ปีที่แล้ว +1

    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.

  • @AnuragSharma-wb1fc
    @AnuragSharma-wb1fc 6 ปีที่แล้ว +1

    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  6 ปีที่แล้ว

      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 6 ปีที่แล้ว

      thanks mark , your videos are mind blowing . Its really helpful.

  • @testuser10
    @testuser10 6 ปีที่แล้ว +1

    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!

  • @marcelagoncalvesteixeirade3674
    @marcelagoncalvesteixeirade3674 6 ปีที่แล้ว +1

    Good good !!!!👏👏👏👏😃😃😃

  • @tenminutetokyo2643
    @tenminutetokyo2643 6 ปีที่แล้ว +1

    DOOD!