!!! Listing zum Artikel "Fallenstudie"
!!! von Andreas Oetjen in iX 1/2011,  S. 106

!!! Listing 1: Einheitlicher dealloc-Code
durch set-Methoden

@property (nonatomic, retain) NSString *retainValue;
@property (nonatomic, copy) NSString *copyValue;
@property (nonatomic, assign) NSString *assignValue;
// ...
- (void) dealloc {
[self setRetainValue:nil];
[self setAssignValue:nil];
[self setCopyValue:nil];
[super dealloc];
}

!!! Listing 2: Geheimer Code des Autors erwartet

@interface SecretClass {}
- secretMethod:(int)aValue withSecret:(int[]) secretKeys;
@end
@implementation SecretClass
- secretMethod:(int)aValue withSecret:(int[]) secretKeys {
    if (! checkValidKey(secretKeys)) // C-Funktion, keine Nachricht!
      return;
    // Hier steht der tatsächliche Code
}
@end

!!! Listing 3: Exception-Objekt verfrüht freigegeben

Klasse *obj = [[Klasse alloc] init];
@try {
    [obj doSomething];
} @finally {
    [obj release];
    [myPool drain];
}

!!! Listing 4: Autorelease-Pool im Exception-Fall nicht geleert

NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
Klasse *obj = [[Klasse alloc] init];
@try {
    [obj doSomething];
} @finally {
    [obj release];
}
[myPool drain];

!!! Listing 5: Exception an äußeren Autorelease-Pool gehängt

NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
Klasse *obj = [[Klasse alloc] init];
id innerEx;
@try {
    [obj doSomething];
} @catch(id exception) {
    innerEx = [exception retain]; // innerEx.retainCount == 2
    @throw;
} @finally {
    [obj release];
    [myPool drain]; // innerEx.retainCount == 1
    [innerEx autorelease]; // Übergabe an äußeren Pool
}

!!! Listing 6: Handler für nichtgefangene Exceptions

#import <ExceptionHandling/NSExceptionHandler.h>
int aMask = NSLogUncaughtExceptionMask
    | NSHandleUncaughtExceptionMask
    | NSLogUncaughtSystemExceptionMask
    | NSHandleUncaughtSystemExceptionMask
    | NSLogUncaughtRuntimeErrorMask
    | NSHandleUncaughtRuntimeErrorMask;
[[NSExceptionHandler defaultExceptionHandler]
    setExceptionHandlingMask: aMask];
[[NSExceptionHandler defaultExceptionHandler]
    setDelegate: self];
