Skip to content

Instantly share code, notes, and snippets.

@joerick
Created February 11, 2012 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerick/1799355 to your computer and use it in GitHub Desktop.
Save joerick/1799355 to your computer and use it in GitHub Desktop.
KVO/KVC crash on reloading unloaded bundles
//////////////////////// SIAppDelegate.h ////////////////////////////
#import <Cocoa/Cocoa.h>
@interface SIAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@end
//////////////////////// SIAppDelegate.m ////////////////////////////
#import "SIAppDelegate.h"
#import "Bundle.h"
@implementation SIAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"testbundle" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath: bundlePath];
for (int i = 0; i < 100; i++)
{
[bundle load];
Class bundleClass = [bundle principalClass];
[[[bundleClass alloc] init] release];
[bundle unload];
}
}
@end
//////////////////////// Bundle.h ////////////////////////////
#import <Foundation/Foundation.h>
@interface Bundle : NSObject
@property (retain) NSString *name;
@end
//////////////////////// Bundle.m ////////////////////////////
#import "Bundle.h"
@implementation Bundle
@synthesize name;
- (id)init
{
self = [super init];
if (self)
{
[self addObserver: self
forKeyPath: @"name"
options: 0
context: nil];
self.name = @"jim";
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"observer fired");
}
- (void)dealloc
{
[self removeObserver: self
forKeyPath: @"name"];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment