title: Mac Screensaver with WebView crashes
body: <p>Hy everybody,</p>
  
  <p>I have a screensaver made with obj-c and cocoa. Everything works fine under OsX 10.6.2 except the following.
  Within my screensaver I have a WebView with some application running. When I try to call my objective-c app (the screensaver) via javascript, I get an error and the screensaver and the system preferences panel crash.</p>
  
  <blockquote>
    <p>System Preferences[86666] 
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException'</p>
    
    <p>reason: '-[NSCFArray drain]: unrecognized selector sent to instance 0x20049b1e0'</p>
    
    <p>*** Call stack at first throw:(<br>
    0   CoreFoundation                      0x00007fff8123a444 __exceptionPreprocess + 180<br>
    1   libobjc.A.dylib                     0x00007fff81f130f3 objc_exception_throw + 45<br>
    2   CoreFoundation                      0x00007fff812931c0 +[NSObject(NSObject) doesNotRecognizeSelector:] + 0<br>
    3   CoreFoundation                      0x00007fff8120d08f <em>forwarding</em> + 751<br>
    4   CoreFoundation                      0x00007fff812091d8 _CF_forwarding_prep_0 + 232 
    5   WebCore                             0x00007fff847adee0 _ZN3JSC8Bindings12ObjcInstance10virtualEndEv + 48<br>
    6   WebCore                             0x00007fff8470d71d _ZN3JSC16RuntimeObjectImp18getOwnPropertySlotEPNS_9ExecStateERKNS_10IdentifierERNS_12PropertySlotE + 397<br>
    7   JavaScriptCore                      0x00007fff80862b66 NK3JSC7JSValue3getEPNS_9ExecStateERKNS_10IdentifierERNS_12PropertySlotE + 486<br>
    )</p>
  </blockquote>
  
  <p>I know this looks like some memory leak, but as you will see in the code, I really have nearly no objects allocated.</p>
  
  <p>This only happens, when I start the screensaver with the "Test" button from the screensaver system prefs.
  When I start the screensaver via terminal or if it starts automatically, the same action (calling obj-c from javascript) works fine.</p>
  
  <p>Maybe someone has any idea, where the error could come from. Here is some code from the implementation:</p>
  
  <pre><code>@implementation ScreensaverView
  
  - (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview {
  
      self = [super initWithFrame:frame isPreview:isPreview];
  
      if (self) {
  
          [self setAnimationTimeInterval:-1];
          [self setAutoresizesSubviews:YES];
  
          // ::::::::::::::::::::::: Init stuff ::::::::::::::::::    
  
          // init 
          quitFlag = false;
          previewMode = isPreview;
  
          // find out the path the screensaver bundle
          pMainBundle = [NSBundle bundleForClass:[self class]];
          pBundlePath = [pMainBundle bundlePath];
  
          // read Info.plist
          infoDict = [pMainBundle infoDictionary];
      }
  
      return self;
  }
  
  - (void)startAnimation
  {   
      [super startAnimation];
  
      // combine: bundle path + filename for screensaver file 
      NSString *pathToScreensaver = [NSString stringWithString:pBundlePath];
      NSString *valueScreensaverFile;
  
      if(!previewMode)
      {
          valueScreensaverFile = [infoDict objectForKey:@"ScreensaverFile"];
      }
      else 
      {
          valueScreensaverFile = [infoDict objectForKey:@"PreviewFile"];
      }
  
      // add filename to bundle path
      pathToScreensaver = [pathToScreensaver stringByAppendingString:valueScreensaverFile];
  
      // complete NSURL to the screensaver file
      NSURL *screensaverUrl = [NSURL fileURLWithPath: pathToScreensaver];
  
      webView = [WebView alloc];
      [webView initWithFrame:[self frame]];
      [webView setDrawsBackground:NO];
  
      // delegation policy for interactive mode
      [webView setPolicyDelegate: self];
      [webView setUIDelegate:self];
  
      // load screensaver
      [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:screensaverUrl]];
  
      scriptObject = [webView windowScriptObject];
      [scriptObject setValue:self forKey:@"screensaver"];
  
      [self addSubview:webView];
  }
  
  - (void)stopAnimation
  {   
      [[webView mainFrame] stopLoading];
      [webView removeFromSuperview];
      [webView release];
      [super stopAnimation];
  }
  
  + (BOOL)isSelectorExcludedFromWebScript:(SEL)selector 
  {       
      if (selector == @selector(quitScreenSaver)) {
          return NO;
      }
  
      if(selector == @selector(gotoUrl:) ){
          return NO;
      }
  
      return YES;
  }
  
  +(NSString *)webScriptNameForSelector:(SEL)selector
  {   
      if(selector == @selector(quitScreenSaver))
      {
          return @"quitNoOpen";
      }
  
      if(selector == @selector(gotoUrl:))
      {
          return @"openAndQuit";
      }
  
      return nil;
  }
  
  - (void) quitScreenSaver
  {
      quitFlag = true;
      [super stopAnimation];
  }
  
  - (void) gotoUrl:(NSString *) destinationURL 
  {   
      if(destinationURL == NULL)
      {
          return;
      }
  
      NSString * path    = destinationURL;
      NSURL    * fileURL = [NSURL URLWithString:path];
      [[ NSWorkspace sharedWorkspace ] openURL:fileURL];
      [self quitScreenSaver];
  }
  
  @end
  </code></pre>
  
  <p>I hope that's enough code for you to see some problems / solutions.
  I would really appreciaty any answers.</p>