การแก้วงจรการยึดครองในบล็อก
วิเคราะห์และแก้ปัญหาหน่วยความจำรั่วที่พบบ่อยที่สุดในแอปเก่า: วงจรการยึดครองที่เกิดจากบล็อกจับ self โดยใช้การอ้างอิงแบบอ่อนและแบบเข้มแข็ง
การแก้วงจรการยึดครองในบล็อก เป็นบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Objective-C iOS Development for Legacy & Enterprise Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is a Retain Cycle?
A retain cycle happens when two objects strongly reference each other. Neither's count can reach zero, so neither is ever freed — a memory leak ARC cannot break for you.
Blocks Capture Strongly
An Objective-C block captures the variables it uses, including self — and it captures them strongly by default. This is the number one source of cycles.
A Cycle in Action
Here self owns the block (stored in a property), and the block strongly captures self. That mutual ownership leaks.
self.completion = ^{
[self refreshUI];
};The weak Qualifier
Break the cycle by capturing a weak reference to self. A weak reference does not increase the retain count.
__weak typeof(self) weakSelf = self;
self.completion = ^{
[weakSelf refreshUI];
};The weakSelf Pattern
The convention names the capture weakSelf using __weak typeof(self) so the type stays correct even if the class changes.
__weak typeof(self) weakSelf = self;weakSelf Can Become nil
A weak reference becomes nil the instant the object deallocates. If the block runs after that, messages to weakSelf are silently ignored — but partial access can be inconsistent.
The strongSelf Guard
Inside the block, promote weakSelf to a temporary strong reference so it cannot vanish mid-execution.
self.completion = ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf) {
[strongSelf refreshUI];
}
};When You Do NOT Need weakSelf
Not every block leaks. If self does not own the block — for example a one-shot block passed to a method and not stored — capturing self strongly is fine and even safer.
[UIView animateWithDuration:0.3 animations:^{
self.view.alpha = 0;
}];Delegate Cycles
The same rule applies to delegates: a delegate property should be weak so a view controller and its child do not retain each other.
@property (nonatomic, weak) id<MyDelegate> delegate;Finding Leaks
Tools to hunt cycles:
- Instruments > Leaks flags leaked allocations
- Memory Graph Debugger in Xcode visualizes retain cycles
- Log in
deallocto confirm objects are freed
Rule of Thumb
Ask: does self own this block, and does the block touch self? If both are yes, use weakSelf (plus a strongSelf guard for multi-step work).
Quick Check
Test your retain cycle knowledge.
Recap
You learned to break retain cycles:
- Blocks capture
selfstrongly by default - A cycle leaks when self owns a block that references self
- Use
__weak typeof(self) weakSelfto break it - Add a
strongSelfguard for multi-step work - Mark delegate properties
weak
Instruments and the Memory Graph confirm your fixes.
คำถามที่พบบ่อย
บทเรียน “การแก้วงจรการยึดครองในบล็อก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแก้วงจรการยึดครองในบล็อก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Objective-C iOS Development for Legacy & Enterprise Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Objective-C iOS Development for Legacy & Enterprise Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแก้วงจรการยึดครองในบล็อก”
วิเคราะห์และแก้ปัญหาหน่วยความจำรั่วที่พบบ่อยที่สุดในแอปเก่า: วงจรการยึดครองที่เกิดจากบล็อกจับ self โดยใช้การอ้างอิงแบบอ่อนและแบบเข้มแข็ง คุณปฏิบัติ Objective-C iOS Development for Legacy & Enterprise Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Objective-C iOS Development for Legacy & Enterprise Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Objective-C iOS Development for Legacy & Enterprise Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การแก้วงจรการยึดครองในบล็อก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Objective-C iOS Development for Legacy & Enterprise Apps นี้ได้ไหม
ได้ บทเรียน Objective-C iOS Development for Legacy & Enterprise Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐานการจัดการหน่วยความจำแบบ Retain-Release (MRR)
- การนับการอ้างอิงอัตโนมัติ (ARC)
- การอ้างอิงแบบ Weak เทียบกับ Strong
- การแก้วงจรการยึดครองในบล็อก