title: Object deallocates immediately after it has been initialized
body: <p>I am trying to make a class that plays YouTube videos, but I am having several problems with it.</p>
  
  <p>Here is my class that handles YouTube videos:</p>
  
  <pre><code>// this is the only property declared in the .h file:
  @property (strong, nonatomic) UIView * view
  
  // the rest of this is the .m file:
  #import "MyYouTube.h"
  @interface MyYouTube()
  @property (strong, nonatomic) NSDictionary * contentData;
  @property (strong, nonatomic) UIWebView * webView;
  @property (nonatomic) int videoOffset;
  @end
  
  @implementation MyYouTube
  @synthesize view,contentData,webView,videoOffset;
  
  - (MyYouTube*) initIntoView: (UIView*) passedView withContent: (NSDictionary*) contentDict {
      NSLog(@"YOUTUBE: begin init");
      self=[super init];
      videoOffset=0;
      view=[[UIView alloc] initWithFrame:passedView.bounds];
      [view setBackgroundColor:[UIColor blackColor]];
      [view setAutoresizesSubviews:YES];
      [view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
      contentData=contentDict;
  
      NSString * compiledUrl=[[NSString alloc] initWithFormat:@"http://_xxx_.com/app/youtube.php?yt=%@",[contentData objectForKey:@"cnloc"]];
      NSURL * url=[[NSURL alloc] initWithString:compiledUrl];
      NSURLRequest * request=[[NSURLRequest alloc] initWithURL:url];
  
      webView=[[UIWebView alloc] initWithFrame:passedView.bounds];
      [webView loadRequest:request];
      [webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
      [[webView scrollView] setScrollEnabled:NO];
      [[webView scrollView] setBounces:NO];
      [webView setMediaPlaybackRequiresUserAction:NO];
      [webView setDelegate:self];
  
      NSLog(@"YOUTUBE: self: %@",self);
      NSLog(@"YOUTUBE: delegate: %@",webView.delegate);
  
      [view addSubview:webView];
      NSLog(@"YOUTUBE: end init");
      return self;
  }
  
  -(void)webViewDidFinishLoad:(UIWebView*)myWebView {
      NSLog(@"YOUTUBE: send play command");
      [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"playVideo(%d)", videoOffset]];
  }
  
  - (void) dealloc {
      NSLog(@"YOUTUBE: dealloc");
  }
  @end
  </code></pre>
  
  <p>Here is the code that calls this class (this code is located in the appDelegate):</p>
  
  <pre><code>NSLog(@"about to load youtube");
  ytObj=[[MyYouTube alloc] initIntoView:mainView withContent:cn];
  NSLog(@"loaded youtube");
  [mainView addSubview:[ytObj view]];
  </code></pre>
  
  <p>FYI mainView and ytObj are declared as this:</p>
  
  <pre><code>@property (nonatomic,strong) UIView * mainView;
  @property (nonatomic,strong) MyYouTube * ytObj;
  </code></pre>
  
  <p>When this code is run, the app crashes and I get this in the console:</p>
  
  <pre><code>about to load youtube
  YOUTUBE: begin init
  YOUTUBE: self: &lt;MyYouTube: 0x16503d40&gt;
  YOUTUBE: delegate: &lt;MyYouTube: 0x16503d40&gt;
  YOUTUBE: end init
  YOUTUBE: dealloc
  loaded youtube
  *** -[MyYouTube respondsToSelector:]: message sent to deallocated instance 0x166f19f0
  </code></pre>
  
  <p>If I set the UIWebView delegate to <code>nil</code> then the app doesn't crash, but, as expected, the YouTube video doesn't autoplay.</p>
  
  <p>Can anyone explain to me:</p>
  
  <ul>
  <li><p>Why does the object deallocates immediately after it has been
  initialized?</p></li>
  <li><p>Why the <code>respondsToSelector:</code> message is sent to an
  instance other than the MyYouTube one?</p></li>
  <li><p>How can I get the YouTube video to autoplay without the app crashing?</p></li>
  </ul>
  
  <p>Many thanks.</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>Totally my bad - ytObj is a strong property - I forgot to mention this. Ive added the code to reflect this.</p>
  
  <hr>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Ive added a breakpoint on the dealloc method, and this is the call stack:</p>
  
  <pre><code>0 -[MyYouTube dealloc]
  1 objc-object::sidetable_release(bool)
  2 -[MyAppDelegate playYouTube:]
  </code></pre>
  
  <p>The last entry here (<code>playYouTube:</code>) is the method that contains the code in my app delegate that is in the original post above. So, one more question:</p>
  
  <ul>
  <li>What is <code>objc-object::sidetable_release(bool)</code>, what does it do, and why is it releasing my YouTube object?</li>
  </ul>