Sie sind auf Seite 1von 50

main




alloc newObject mutableCopy retain
release
autorelease

retain release autorelease

■ autorelease release

myInstance
MyClass *myInstance = [MyClass createInstance];

alloc newObject mutableCopy

■ retain

release autorelease
autorelease

{
Thingamajig *myThingamajig = [[Thingamajig alloc] init];
// ...
NSArray *sprockets = [myThingamajig sprockets];
// ...
[myThingamajig release];
}

alloc
release
release

retain

retain
release
autorelease

retainCount

autorelease NSObject
autorelease

sprockets

– (NSArray *)sprockets {

NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket,


auxiliarySprocket, nil];
return [array autorelease];
}

alloc
autorelease
autorelease
sprockets

– (NSArray *)sprockets {

NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket,


auxiliarySprocket, nil];
return array;
}

sprockets

release

– (NSArray *)sprockets {

NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket,


auxiliarySprocket, nil];
[array release];
return array; // array is invalid here
}

sprockets

– (NSArray *)sprockets {

NSArray *array = [NSArray arrayWithObjects:mainSprocket,


auxiliarySprocket, nil];
return array;
}

arrayWithObjects:
sprockets
arrayWithObjects:
autorelease

heisenObject = [array objectAtIndex:n];


[array removeObjectAtIndex:n];
// heisenObject could now be invalid.

release
autorelease
heisenObject

id parent = <#create a parent object#>;


// ...
heisenObject = [parent child] ;
[parent release]; // Or, for example: self.parent = nil;
// heisenObject could now be invalid.

heisenObject
release autorelease dealloc

heisenObject

heisenObject = [[array objectAtIndex:n] retain];


[array removeObjectAtIndex:n];
// use heisenObject.
[heisenObject release];
setMainSprocket:

– (void)setMainSprocket:(Sprocket *)newSprocket {
[mainSprocket autorelease];
mainSprocket = [newSprocket retain]; /* Claim the new Sprocket. */
return;
}

setMainSprocket:

copy

– (void)setMainSprocket:(Sprocket *)newSprocket {
[mainSprocket autorelease];
mainSprocket = [newSprocket copy]; /* Make a private copy. */
return;
}

newSprocket mainSprocket

– (void)setMainSprocket:(Sprocket *)newSprocket {
if (mainSprocket != newSprocket) {
[mainSprocket release];
mainSprocket = [newSprocket retain]; /* Or copy, if appropriate. */
}
}

dealloc

0
dealloc
dealloc
dealloc
mainSprocket
auxiliarySprocket dealloc

- (void)dealloc {
[mainSprocket release];
[auxiliarySprocket release];
[super dealloc];
}

dealloc

dealloc

ClassName ** id *
NSError

■ initWithContentsOfURL:options:error: NSData

■ initWithContentsOfFile:encoding:error: NSString

■ executeFetchRequest:error: NSManagedObjectContext

NSError

NSString *fileName = <#Get a file name#>;


NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfFile:fileName
encoding:NSUTF8StringEncoding error:&error];
if (string == nil) {
// deal with error ...
}
// ...
[string release];

dataFromPropertyList:format:errorDescription:
Document

page

don’t
Page retain retain

parent
paragraph

Paragraph retain don’t


retain

text
parent
NSTableView
NSApplication

setDelegate: nil
dealloc

dealloc
dealloc dealloc

dealloc

dealloc

1 0

alloc
1
alloc new
copy
1
copy
retain
1

release
1

autorelease
1

■ copy alloc retain


release autorelease


alloc newObject mutableCopy retain

■ dealloc

■ dealloc
dealloc

+className...

alloc

alloc

- (void)printHello {
NSString *string;
string = [[NSString alloc] initWithString:@"Hello"];
NSLog(string);
[string release];
}

- (void)printHello {
NSString *string;
string = [NSString stringWithFormat:@"Hello"];
NSLog(string);
}
- (void)printWindowTitle {
NSString *string;
string = [myWindow title];
NSLog(string);
}

retain release

@interface Counter : NSObject {


NSNumber *count;
}

retain release

- (NSNumber *)count {
return count;
}

retain
release
nil
[newCount retain]

- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[count release];
// make the new assignment
count = newCount;
}

init
dealloc
init

- init {
if (self = [super init]) {
count = [[NSNumber alloc] initWithInteger:0];
}
return self;
}
initWithCount:

- initWithCount:(NSNumber *)startingCount {
if (self = [super init]) {
count = [startingCount copy];
}
return self;
}

dealloc
release

- (void)dealloc {
[count release];
[super dealloc];
}

NSNumber retain
release

- (void)reset {
NSNumber *zero = [NSNumber numberWithInteger:0];
[self setCount:zero];
}

NSNumber alloc release

- (void)reset {
NSNumber *zero = [[NSNumber alloc] initWithInteger:0];
[self setCount:zero];
[zero release];
}

- (void)reset {
NSNumber *zero = [[NSNumber alloc] initWithInteger:0];
[count release];
count = zero;
}
- (void)reset {
NSNumber *zero = [[NSNumber alloc] initWithInteger:0];
[self setCount:zero];
}

alloc

- (void)reset {
NSNumber *zero = [NSNumber numberWithInteger:0];
[self setCount:zero];
[zero release];
}

retain count

release autorelease

SIGBUS 10

NSMutableArray *array;
NSUInteger i;
// ...
for (i = 0; i < 10; i++) {
NSNumber *convenienceNumber = [NSNumber numberWithInteger:i];
[array addObject:convenienceNumber];
}

alloc release
convenienceNumber

NSMutableArray *array;
NSUInteger i;
// ...
for (i = 0; i < 10; i++) {
NSNumber *allocedNumber = [[NSNumber alloc] initWithInteger: i];
[array addObject:allocedNumber];
[allocedNumber release];
}

allocedNumber release for


alloc addObject:

retain release
release dealloc
NSAutoreleasePool
autorelease release
release
autorelease release

NSAutoreleasePool alloc init


release drain autorelease retain
release drain

autorelease

main


main()

main

main

void main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSArray *args = [[NSProcessInfo processInfo] arguments];

for (NSString *fileName in args) {

NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];

NSError *error = nil;


NSString *fileContents = [NSString stringWithContentsOfFile:fileName
encoding:NSUTF8StringEncoding
error:&error];

/* Process the string, creating and autoreleasing more objects. */

[loopPool release];
}

/* Do whatever cleanup is needed. */


[pool drain];

exit (EXIT_SUCCESS);
}

for
NSAutoreleasePool
autorelease fileContents loopPool
loopPool
for fileName loopPool
autorelease

NSAutoreleasePool

NSAutoreleasePool
NSThread
NSThread
NSThread isMultiThreaded

autorelease
addObject:

release
autorelease
release

retain
autorelease

– findMatchingObject:anObject
{
id match = nil;

while (match == nil) {


NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];

/* Do a search that creates a lot of temporary objects. */


match = [self expensiveSearchForObject:anObject];

if (match != nil) {
[match retain]; /* Keep match around. */
}
[subPool release];
}

return [match autorelease]; /* Let match go and return it. */


}

retain match subpool autorelease subpool


match subpool
match
findMatchingObject:
release NSAutoreleasePool drain
release

drain release
@property (copy) NSString *firstName;
@property (readonly) NSString *fullName;
@property (retain) NSDate *birthday;
@property NSInteger luckyNumber;

@synthesize firstName;
@synthesize fullName;
@synthesize birthday;
@synthesize luckyNumber;

nonatomic
- (NSString*) title {
return [[title retain] autorelease];
}

- (void) setTitle: (NSString*) newTitle {


if (title != newTitle) {
[title release];
title = [newTitle retain]; // or copy depending on your needs
}
}

- (NSString*) title {
return title;
}

- (void) setTitle: (NSString*) newTitle {


[title autorelease];
title = [newTitle retain];
}

- (NSString*) title {
return title;
}

- (void) setTitle: (NSString*) newTitle {


if (newTitle != title) {
[title release];
title = [newTitle retain];
}
}
NSString

name

- (void)setName:(NSString *)aName {
[name autorelease];
name = [aName copy];
}

aName

name

- (NSString *)name {
return [[name copy] autorelease];
}
NSCopying copyWithZone:

NSCopying
copyWithZone: alloc init... NSCopyObject

■ NSCopying

- (void)setMyVariable:(id)newValue
{
[myVariable autorelease];
myVariable = [newValue copy];
}

- (void)setMyVariable:(id)newValue
{
[myVariable autorelease];
myVariable = [newValue retain];
}
- (void)setMyVariable:(id)newValue
{
myVariable = newValue;
}

@interface Product : NSObject <NSCopying>


{
NSString *productName;
float price;
id delegate;
}

@end

NSCopying

productName
delegate

original 0xf2ae4 copy 0x104074

isa 0x8028 isa 0x8028


productName 0xf2bd8 productName 0xe81f4
price 0.00 price 0.00
delegate 0xe83c8 delegate 0xe83c8

productName
productName delegate

NSCopying

alloc init... set


NSCopying
copyWithZone:

NSCopyObject alloc
init...

NSCopying copyWithZone: alloc


init... copyWithZone:

- (id)copyWithZone:(NSZone *)zone
{
Product *copy = [[[self class] allocWithZone: zone]
initWithProductName:[self productName]
price:[self price]];
[copy setDelegate:[self delegate]];

return copy;
}

NSCopying alloc init...

NSCopying
NSCopyObject NSCopyObject
NSCell
copyWithZone:

- (id)copyWithZone:(NSZone *)zone
{
NSCell *cellCopy = NSCopyObject(self, 0, zone);
/* Assume that other initialization takes place here. */

cellCopy->image = nil;
[cellCopy setImage:[self image]];

return cellCopy;
}

NSCopyObject
copyWithZone: image
setImage:

- (void)setImage:(NSImage *)anImage
{
[image autorelease];
image = [anImage retain];
}

setImage: image
copyWithZone: image nil setImage:

image
alloc init... nil
cellCopy image
nil setImage:

NSCopyObject
NSSliderCell titleCell

- (id)copyWithZone:(NSZone *)zone
{
id cellCopy = [super copyWithZone:zone];
/* Assume that other initialization takes place here. */

cellCopy->titleCell = nil;
[cellCopy setTitleCell:[self titleCell]];

return cellCopy;
}

super copyWithZone

id copy = [[[self class] allocWithZone: zone] init];

copyWithZone:
copyWithZone:
NSCopyObject
titleCell nil setTitleCell:

NSCopyObject
copyWithZone:
original 0xf2ae4 copy 0x104074 copy 0x104074

isa 0x8028 isa 0x8028 isa 0x8028


refCount 3 refCount 3 refCount 1
productName 0xf2bd8 productName 0xf2bd8 productName 0xe81f4
price 0.00 price 0.00 price 0.00
delegate 0xe83c8 delegate 0xe83c8 delegate 0xe83c8

The copy produced by The copy after unitialized


NSCopyObject instance variables are assigned
in copyWithZone:

refCount

NSCopyObject refCount
copyWithZone: refCount copyWithZone:
NSCopyObject refCount
copyWithZone:

NSCopying
NSCopying
NSCopying
retain copyWithZone:

- (id)copyWithZone:(NSZone *)zone {
return [self retain];
}

NSMutableCopying
mutableCopyWithZone:
NSObject mutableCopy
mutableCopyWithZone:
CFString
NSString

NSString *str = [[NSString alloc] initWithCharacters: ...];


...
[str release];

CFStringRef str = CFStringCreateWithCharacters(...);


...
CFRelease(str);

NSString *str = (NSString *)CFStringCreateWithCharacters(...);


...
[str release];

NSString *str = (NSString *)CFStringCreateWithCharacters(...);


...
[str autorelease];
@property (attributes) IBOutlet UserInterfaceElementClass *anOutlet;

@property (assign) IBOutlet UserInterfaceElementClass *anOutlet;

@property (nonatomic, retain) IBOutlet UIUserInterfaceElementClass *anOutlet;

dealloc

NSApp
dealloc NSApp

■ NSWindow isReleasedWhenClosed YES

■ NSWindowController
NSDocument NSWindowController

NSWindowController
NSWindowController

instantiateNibWithOwner:topLevelObjects: NSNib

dealloc

NSWindowController

NSWindowController

setValue:forKey:
loadNibNamed:owner:options:

didReceiveMemoryWarning

didReceiveMemoryWarning UIViewController [self setView:nil]

didReceiveMemoryWarning
viewDidUnload
nil

- (void)viewDidUnload {
self.anOutlet = nil;
[super viewDidUnload];
}

viewDidUnload
nil setView:
- (void)setView:(UIView *)aView {
if (!aView) { // View is being set to nil.
// Set outlets to nil, e.g.
self.anOutlet = nil;
}
// Invoke super's implementation last.
[super setView:aView];
}

dealloc UIViewController
nil dealloc
- (void)dealloc {
// Release outlets and set outlet variables to nil.
[anOutlet release], anOutlet = nil;
[super dealloc];
}

Das könnte Ihnen auch gefallen