官术网_书友最值得收藏!

An object's ownership and life cycle

The idea of object ownership abstraction is simple—one entity is simply responsible for another and an entity has the ability to own an object. When an entity owns an object, the entity is responsible to free that object too.

Let's go to our code example. If an object was created and used in the main function, then the main function is responsible for the object, as the following code listing demonstrates:

int main(int argc, char *argv[]) {

  SomeObject *myOwnObject;
  // myOwnObject is created in main
   myOwnObject = [[SomeObject alloc] init];

    // myOwnObject can be used by other objects
  [anotherObject using:myOwnObject];
    
    // but main is responsible for releasing it
   [myOwnObject release];

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

What makes this concept a bit more complicated is that objects can be owned by more than one entity. So, an object may be created and owned in the main function and will also be used by another entity that will claim ownership of the object.

A common situation where you will see multiple object ownership is when you use arrays. Arrays are indexed lists of objects, and when an object is placed into an array, the array claims ownership of the object. So, if I create an object in the main function and then put that object into an array, both the main function and the array will claim ownership of the object and create a reference to it at the same time. Ownership and reference are different as an object references another object, which it does not own and both are responsible for cleaning up the object. The following code demonstrates this:

int main (int argc, char *argv[]) {

  SomeObject *myOwnObject;
  // myOwnObject is created in main
myOwnObject = [[SomeObject alloc] init];

// myOwnObject can be used by other objects
NSMutableArray *myArray;
// add my object to myArray    
myArray = [[NSMutableArray alloc] initWithObjects:myOwnObject, nil];
    
// main does not need myOwnObject any more
[myOwnObject release];

// but myOwnObject still is needed inside the array
[anotherObj usingArray: myArray];

Just like objects in the real world, Objective-C objects are created; they live, and then go away when the application is closed. This is how the object life cycle works. Obviously, arrays have to claim the ownership on the object and prevent it to be deleted in the release method called in the main function.

However, what is the correct way for the entity to claim its rights on an object that it owns? Let's take a deeper look at the problem.

主站蜘蛛池模板: 敦煌市| 上犹县| 平江县| 平舆县| 淮滨县| 即墨市| 包头市| 石楼县| 永胜县| 胶南市| 柳河县| 华阴市| 上林县| 齐齐哈尔市| 神池县| 阳信县| 方山县| 南岸区| 辽中县| 驻马店市| 米脂县| 滨海县| 永福县| 乌恰县| 阿拉善右旗| 偏关县| 葫芦岛市| 南召县| 周至县| 石景山区| 马山县| 湘阴县| 保康县| 文成县| 肥城市| 荆门市| 界首市| 青浦区| 叶城县| 古浪县| 拉孜县|