没有合适的资源?快使用搜索试试~ 我知道了~
首页Memory_Management_Programming_Guide_for_Cocoa.pdf 英文版
Memory_Management_Programming_Guide_for_Cocoa.pdf 英文版

Introduction In any program you write, you must ensure that you manage resources effectively and efficiently. One such resource is your program’s memory. In an Objective-C program, you must make sure that objects you create are disposed of when you no longer need them. In a complex system, it could be difficult to determine exactly when you no longer need an object. Cocoa defines some rules and principles that help making that determination easier.
资源详情
资源评论
资源推荐

Memory Management Programming Guide
for Cocoa
Cocoa > Objective-C Language
2009-07-23

Apple Inc.
© 2009 Apple Inc.
All rights reserved.
No part of this publication may be reproduced,
stored in a retrieval system, or transmitted, in
any form or by any means, mechanical,
electronic, photocopying, recording, or
otherwise, without prior written permission of
Apple Inc., with the following exceptions: Any
person is hereby authorized to store
documentation on a single computer for
personal use only and to print copies of
documentation for personal use provided that
the documentation contains Apple’s copyright
notice.
The Apple logo is a trademark of Apple Inc.
Use of the “keyboard” Apple logo
(Option-Shift-K) for commercial purposes
without the prior written consent of Apple may
constitute trademark infringement and unfair
competition in violation of federal and state
laws.
No licenses, express or implied, are granted
with respect to any of the technology described
in this document. Apple retains all intellectual
property rights associated with the technology
described in this document. This document is
intended to assist application developers to
develop applications only for Apple-labeled
computers.
Every effort has been made to ensure that the
information in this document is accurate. Apple
is not responsible for typographical errors.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
.Mac is a registered service mark of Apple Inc.
Apple, the Apple logo, Carbon, Cocoa, Mac,
Mac OS, Objective-C, and Xcode are trademarks
of Apple Inc., registered in the United States
and other countries.
iPhone is a trademark of Apple Inc.
Simultaneously published in the United States
and Canada.
Even though Apple has reviewed this document,
APPLE MAKES NO WARRANTY OR REPRESENTATION,
EITHER EXPRESS OR IMPLIED, WITH RESPECT TO
THIS DOCUMENT, ITS QUALITY, ACCURACY,
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR
PURPOSE. AS A RESULT, THIS DOCUMENT IS
PROVIDED “AS IS,” AND YOU, THE READER, ARE
ASSUMING THE ENTIRE RISK AS TO ITS QUALITY
AND ACCURACY.
IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES RESULTING FROM ANY
DEFECT OR INACCURACY IN THIS DOCUMENT, even
if advised of the possibility of such damages.
THE WARRANTY AND REMEDIES SET FORTH ABOVE
ARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORAL
OR WRITTEN, EXPRESS OR IMPLIED. No Apple
dealer, agent, or employee is authorized to make
any modification, extension, or addition to this
warranty.
Some states do not allow the exclusion or limitation
of implied warranties or liability for incidental or
consequential damages, so the above limitation or
exclusion may not apply to you. This warranty gives
you specific legal rights, and you may also have
other rights which vary from state to state.

Contents
Introduction 7
Who Should Read This Document 7
Organization of This Document 7
Object Ownership and Disposal 9
Object Ownership Policy 9
Creating Objects Using Convenience Methods 10
Objects Returned by Reference 11
Delayed Release 11
Taking Ownership of Objects 12
Validity of Shared Objects 12
Retain Cycles 13
Weak References to Objects 14
Retain Count 15
Deallocating an Object 15
Resource Management 16
Summary 16
Practical Memory Management 17
Basics 17
Simple Examples 18
Using Accessor Methods 19
Implementing a reset method 20
Common Mistakes 20
Cases which Often Cause Confusion 21
Autorelease Pools 23
Overview of Autorelease Pools 23
Autorelease Pools in Non-AppKit Programs 24
Autorelease Pools and Threads 25
Scope of Autorelease Pools and Implications of Nested Autorelease Pools 25
Guaranteeing the Foundation Ownership Policy 26
Garbage Collection 27
Accessor Methods 29
Accessor Method Fundamentals 29
Declaring Accessor Methods 29
3
2009-07-23 | © 2009 Apple Inc. All Rights Reserved.

Implementing Accessor Methods 29
Technique 1 30
Technique 2 30
Technique 3 30
Value Objects and Copying 31
Implementing Object Copy 33
Deep Versus Shallow Copies 33
Independent Copy 34
Inheriting NSCopying from the Superclass 34
Using the “alloc, init...” Approach 35
Using NSCopyObject() 35
Copying Mutable Versus Immutable Objects 37
Memory Management of Core Foundation Objects in Cocoa 39
Using Memory Zones 41
Creating and Managing Zones 41
Allocating Memory in Zones 42
Memory Management of Nib Objects 43
Outlets 43
Mac OS X Desktop 43
iPhone 44
Top-Level Objects 44
Memory Warnings 45
Memory Management Rules 47
Document Revision History 49
4
2009-07-23 | © 2009 Apple Inc. All Rights Reserved.

Figures and Listings
Object Ownership and Disposal 9
Figure 1 An illustration of retain cycles 14
Autorelease Pools 23
Listing 1 Example of a main function for a non-AppKit program 24
Implementing Object Copy 33
Figure 1 Copying instance variables both shallowly and deeply 34
Figure 2 Initialization of the reference count during a copy 37
5
2009-07-23 | © 2009 Apple Inc. All Rights Reserved.
剩余49页未读,继续阅读














安全验证
文档复制为VIP权益,开通VIP直接复制

评论4