What is Retain Count or Reference Counting?

Retain Count is the term which is related to the Memory Management . whenever you create a object, Objective C doesn't really care about the object you have created , the area of memory is claimed for this object and retain count is increased by one.

                     YourClass *yourObj  = [ YourClass alloc] init];
                      [obj someMethod];
                        [yourObj release];

whenever you release that object the retain count does  go down by one ,whenever the retain count reaches zero, Objective C blows that object and it will be ready to reclaim that memory for another Object.
                   YourClass *yourObj  = [ YourClass alloc] init];
                   [someOtherObj someMethod];
                   [yourObj release];
whenever you pass  "yourObj" to  someMethod of someOtherObj, then a retain message is passed to that object and retain count increased to two. someOtherObj will take care of reducing the retain count to one when it is done and the regular release that you made will reduce it to zero.

Here "yourObj" is the pointer variable that stores the memory address of the object (or) simply it is reference to the object. Even after the object is blown from the memory address of the memory still present in "yourObj" variable called dangling pointer but it refers to the nil object.

if you want to learn about what happens when you pass a message to nil Object, you can follow this link

1 comment:

  1. Top SEO Company The best known mobile App design and development company based in Lahore Pakistan. Get applications for Android, iOS, HTML5, Windows.
    IOS Apps Development in Lahore