0
0
Swiftprogramming~20 mins

Debugging memory leaks with Instruments in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code snippet?

Consider the following Swift code that creates a class with a closure capturing self. What will be printed when printMessage() is called?

Swift
class Printer {
    var message = "Hello"
    lazy var printClosure: () -> Void = {
        print(self.message)
    }
}

let printer = Printer()
printer.printClosure()
ACompilation error due to capturing self
BHello
CRuntime crash due to unowned self
Dnil
Attempts:
2 left
💡 Hint

Think about how closures capture self strongly by default in Swift.

🧠 Conceptual
intermediate
2:00remaining
Which scenario causes a retain cycle in Swift?

Choose the scenario that will cause a retain cycle leading to a memory leak.

AA struct holds a reference to a class instance.
BA class instance holds a weak reference to another class instance.
CA class instance holds a strong reference to a closure that captures self strongly.
DA closure captures a local variable by value.
Attempts:
2 left
💡 Hint

Retain cycles happen when two objects hold strong references to each other.

🔧 Debug
advanced
2:00remaining
Identify the cause of the memory leak in this Swift code

Given the following code, what is the main cause of the memory leak?

Swift
class ViewController {
    var closure: (() -> Void)?

    func setup() {
        closure = {
            print(self)
        }
    }

    deinit {
        print("ViewController deinitialized")
    }
}

var vc: ViewController? = ViewController()
vc?.setup()
vc = nil
AThe deinit method is missing causing no cleanup.
BThe closure is not assigned properly causing a nil reference.
CThe ViewController instance is never created.
DThe closure captures self strongly causing a retain cycle.
Attempts:
2 left
💡 Hint

Think about how closures capture variables and how that affects object lifetime.

📝 Syntax
advanced
2:00remaining
Which option correctly fixes the retain cycle in this Swift closure?

Given the code below, which option correctly fixes the retain cycle by modifying the closure?

class MyClass {
    var closure: (() -> Void)?

    func configure() {
        closure = {
            print(self)
        }
    }
}
Aclosure = { [unowned self] in print(self) }
Bclosure = { [weak self] in print(self) }
Cclosure = { [weak self] in print(self!) }
Dclosure = { print(self) }
Attempts:
2 left
💡 Hint

Consider how to capture self without creating a strong reference but still safely use it.

🚀 Application
expert
2:00remaining
How to detect a memory leak using Instruments in Xcode?

You suspect your Swift app has a memory leak caused by retain cycles. Which step correctly describes how to detect this leak using Instruments?

ARun the app with the Leaks instrument, perform actions, and check for persistent memory growth and leaked objects.
BUse the Time Profiler instrument to find slow functions causing leaks.
CRun the app with the Allocations instrument and ignore leaked objects.
DUse the Network instrument to monitor memory usage.
Attempts:
2 left
💡 Hint

Leaks instrument helps find objects that are not released properly.