0
0
Swiftprogramming~10 mins

Deinitializers for cleanup in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Deinitializers for cleanup
Create instance
Use instance
Instance no longer needed
Deinitializer called
Cleanup done
Memory freed
When an instance is no longer needed, Swift calls its deinitializer to clean up resources before freeing memory.
Execution Sample
Swift
class Person {
    var name: String
    init(name: String) {
        self.name = name
        print("\(name) is initialized")
    }
    deinit {
        print("\(name) is being deinitialized")
    }
}
var person: Person? = Person(name: "Anna")
person = nil
This code creates a Person instance, then sets it to nil, triggering the deinitializer to run.
Execution Table
StepActionInstance StateOutput
1Create Person instance with name 'Anna'Person(name: "Anna") exists"Anna is initialized"
2person variable holds the instancePerson(name: "Anna") exists
3Set person = nil (remove reference)No instance referenced"Anna is being deinitialized"
4Instance deinitialized and memory freedNo instance
💡 person set to nil, no references remain, deinitializer runs and instance is cleaned up
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
personnilPerson(name: "Anna")Person(name: "Anna")nilnil
Key Moments - 2 Insights
Why does the deinitializer run only after setting person to nil?
Because the instance is kept alive as long as 'person' references it. When 'person' is set to nil (see execution_table step 3), no references remain, so deinit runs.
Can we call deinit manually?
No, Swift calls deinit automatically when the instance is about to be destroyed, as shown in step 3 and 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at Step 1?
A"Anna is initialized"
B"Anna is being deinitialized"
CNo output
DError
💡 Hint
Check the Output column at Step 1 in the execution_table.
At which step does the instance get deinitialized?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Action and Output columns in the execution_table for when deinit message appears.
If we never set person to nil, what happens to the deinitializer?
AIt runs immediately after initialization
BIt never runs because the instance is still referenced
CIt runs twice
DIt runs before initialization
💡 Hint
Refer to variable_tracker and execution_table steps showing when references are removed.
Concept Snapshot
Deinitializers run automatically when an instance is about to be destroyed.
Use deinit to clean up resources like files or network connections.
Deinit cannot be called manually.
Instance must have no references for deinit to run.
Setting variables to nil removes references and triggers deinit.
Full Transcript
This visual trace shows how Swift calls a deinitializer to clean up an instance. First, a Person instance named 'Anna' is created and initialized, printing a message. The variable 'person' holds this instance. When 'person' is set to nil, the instance has no references left. At this moment, Swift automatically calls the deinitializer, printing a cleanup message. Finally, the instance is removed from memory. This process ensures resources are freed properly without manual intervention.