Book Image

Objective C Memory Management Essentials

Book Image

Objective C Memory Management Essentials

Overview of this book

Table of Contents (18 chapters)
Objective-C Memory Management Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Autorelease pool mechanism


There will be times when you need to renounce an object's ownership and a good way to do it is by using autorelease pool blocks. Those blocks provide a mechanism where you can renounce it and avoid any chance of the object's immediate deallocation. Even if sometimes you will need to create your own blocks, or it will be in your advantage to do this way, you normally don't need to create them, but there are situations where you may need it.

As in the following code, an autorelease pool block is marked by the usage of @autoreleasepool:

@autoreleasepool {
     //-----
	 // Here you create autoreleased objects.
	 //-----
}

Objects that were created inside the block receive a release message when the block is terminated. An object receives release messages as many times as it receives an autorelease message inside the block.

Autorelease pool blocks can be nested as well:

@autoreleasepool {
    // . . .
    @autoreleasepool {
        // . . .
    }
    //. . .
}

If an autorelease...