Swift – Deinitialization


Swift – Deinitialization



”;


Deinitialization is the process of releasing memory resources when an instance of the class is not required. Deinitializers work only with classes, not with the structure or enumeration. It is not necessary that Swift will immediately call a deinitializer when the instance is no longer needed, it will be called before the instance is deallocated and this timing is managed by the automatic reference counting(ARC) system.

  • A class can have only one deinitializer.

  • Deinitializers are automatically called developers are not allowed to call them manually

  • The timing of calling the deinitializer is not explicitly controlled by the developer. It is automatically determined by the ARC.

  • Circular references can prevent deinitialization.

  • The deinitializer of the superclass is inherited by their subclasses and the deinitializer of the superclass is called after the implementation of the subclass deinitializer(even if the subclass does not have its own deinitializer).

  • As we know the instance is not deallocated until after its deinitializer is called, so it means that the deinitialzer can access and modify the instance’s property.

Defining Deinitializer in Class

In Swift, we can define a deinitializer in a class using “deinit” keyword. The code present inside the deinitializer will execute when the instance of the class is deallocated. The Deinitializer does not take any parameter.

Syntax

Following is the syntax of the deinitializer


class className{
   // Properties and methods of class
   // Deinitializer
   deinit {
      // statement
   }
}

Example

Swift program to create deinitializer.


// Defining class
class Student {

   // Property of class
   var name: String
    
   // Initializer to initialize instance
   init(name: String) {
      self.name = name
      print("(name) is initialized")
   }
    
   // Deinitializer to deinitialize instance
   deinit {
      print("(name) is deinitialized")
   }
}

// Creating and initializing instances of the student class 
var object1: Student? = Student(name: "Mira")
var object2: Student? = Student(name: "Jenni")

// Deinitializers will be called when instances are deallocated
object1 = nil
object2 = nil

Output

It will produce the following output −


Mira is initialized
Jenni is initialized
Mira is deinitialized
Jenni is deinitialized

Deallocating Memory Space Using Deinitializer

In Swift, deinitialization is handled by the Automatic Reference Counting(ARC). Automatic Reference Counting is a memory management mechanism that is used to keep track of the instance references and deallocate objects when they are no longer needed. Although Swift automatically deallocates memory resources we are not required to perform manual cleaning, but if we are using our resources, then we may have to clean them up manually. For example, if we create a custom class to open a file and write some data to it, we might need to close the file before the class instance is deallocated.

Now, we will see how the deinitializer works −

  • Step 1 − Create and initialize a new instance of the class using “init” initializer.

  • Step 2 − Use the instance of the class with strong references. The instance of the class will stay alive as long as there is at least one strong reference to that object.

  • Step 3 − When the last strong reference to an object is terminated at that time deinitializer will be automatically called by the ARC before the object is deallocated.

  • Step 4 − Now the code present in the deinitializer is executed and the instance of the given class will be deallocated. The memory is free for other objects.

So this is how deinitializer works.

Example

Swift program to demonstrate how deinitializer works.


var counter = 0;  // for reference counting
class baseclass {
   init() {
      counter++;
   }
   deinit {
      counter--;
   }
}

var print: baseclass? = baseclass()
print(counter)
print = nil
print(counter)

Output

It will produce the following output −


1
0

When print = nil statement is omitted the values of the counter remain the same since it is not deinitialized.

Example


var counter = 0;  // for reference counting

class baseclass {
   init() {
      counter++;
   }
    
   deinit {
      counter--;
   }
}

var print: baseclass? = baseclass()

print(counter)
print(counter)

Output

It will produce the following output −


1
1

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *