10. Managing Memory in Objective-C

In this chapter, you’ll learn about memory management in Objective-C. Objective-C keeps track of all the objects you create with a retain count, and when that count goes down to zero, Objective-C automatically deallocates the memory allocated to an object.

For example, say that you create two objects:

Class1 *object1 = [[Class1 alloc]  init];Class1 *object2 = [[Class1 alloc]  init];

Now the retain count of each object is 1, as you can verify by asking each object what its retain count is and printing that result:

printf("object1 retain count: %i\n",  [object1 retainCount]);printf("object2 retain count: %i\n",  [object2 retainCount]);

You can also explicitly increment the retain count yourself, like this:.

Get Objective-C: Visual QuickStart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.