Why unloaded class is still accessible in Cocoa?
I loaded a class dynamically with [NSBundle load]
. And unloaded it dynamically with [NSBundle unload]
. Anyway it looks the class is still alive after unloading.
My code is:
// In separated bundle.
@implementation EEExampleBundle
+ (void)test
{
NSLog(@"TTTTT");
}
@end
// In executable file.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool
{
id EEExampleBundle = nil;
开发者_如何学运维 @autoreleasepool
{
NSString* path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"EEExampleBundle.framework"];
NSBundle* sampleBundle = [NSBundle bundleWithPath:path];
[sampleBundle load];
EEExampleBundle = (id)[sampleBundle classNamed:@"EEExampleBundle"];
[EEExampleBundle test];
BOOL r = [sampleBundle unload];
NSLog(@"unload result = %d", r);
}
[EEExampleBundle test];
}
return 0;
}
The output is:
2011-09-25 01:08:52.713 driver[2248:707] TTTTT
2011-09-25 01:08:52.714 driver[2248:707] unload result = 1
2011-09-25 01:08:52.716 driver[2248:707] TTTTT
Why the class code is still working? Is this normal? Or should I do any extra step to unload the code completely?
P.S I'm not using ARC. I turned it off explicitly.
(more of a comment than an answer, nevertheless:) That's due to the inner @autoreleasepool block, no? You won't be able to create a new instance from your bundle, but you do keep the ones already created alive (else, that'd generate fancy bugs).
精彩评论