title: Should I implement IDispose for UserControl
body: <p>I wrote an usercontrol which show the Google Admod to my page. this control is simple with only 1 grid (LayoutRoot), 1 image (get random from my DB) to show when can't get the ad. On each page I have this control, I request a new ad and add to this LayoutRoot. Problem is, create this usercontrol repeatly (when navigate between pages has this control ) cause memory leak (I'm not sure because of the background image or the google admob). So now I want to implement this control to Idispose, but I'm still confused:</p>
  
  <ol>
  <li>Can I (and do I need to) implement Idispose for UserControl ?</li>
  <li>If I do, will my page call the dispose of this control when the page is finalized ?</li>
  <li><p>What is the correct way to implement IDispose ? follow <a href="http://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx" rel="nofollow">this</a> I wrote a test, but I'm not sure if this'is correct ?</p>
  
  <pre><code>bool disposed = false;
  public void Dispose()
  {
      Dispose(true);
      GC.SuppressFinalize(this);
  }
  protected virtual void Dispose(bool disposing)
  {
      if (disposed)
          return;
  
      if (disposing)
      {
          // I'm don't know what to put here ?
      }
      adview.FailedToReceiveAd -= adview_FailedToReceiveAd;
      adview.ReceivedAd -= adview_ReceivedAd;
      adview = null;
      request = null;
      imgMyAd.Source = null;
      imgMyAd = null;
      disposed = true;
  }
  
  ~AdPro()
  {
      Dispose(false);
  }
  </code></pre></li>
  </ol>
  
  <p>here's my UserControl:</p>
  
  <pre><code>&lt;UserControl&gt;
      &lt;Grid x:Name="LayoutRoot" Background="Black" Height="60"&gt;
          &lt;Image x:Name="imgMyAd" Height="{Binding Height ,ElementName=LayoutRoot}"
                  Width="{Binding Width ,ElementName=LayoutRoot}" Stretch="Fill"/&gt;            
          &lt;Grid x:Name="AdMob"&gt;                          
          &lt;/Grid&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>c# </p>
  
  <pre><code>public partial class AdPro : UserControl
  {
      public AdPro()
      {            
          InitializeComponent();
          this.DataContext = App.AdControlD;
          imgMyAd.Source = GetRandomBGImg();     // get the image from my server
          if (App.IsNetworkAvailable)          // check if internet's available
          {                 
              AddAdtoGrid();                
          }
      }
  
      AdRequest request = null;
      AdView adview = null;
  
      public void AddAdtoGrid()
      {
          adview = new AdView()
          {                
              AdUnitID="xxxxxx‏",
              Format = AdFormats.Banner,
              VerticalAlignment = System.Windows.VerticalAlignment.Center,
              HorizontalAlignment = System.Windows.HorizontalAlignment.Center
          };
          AdMob.Children.Add(adview);
          request = new AdRequest();
          request.ForceTesting = true;
          adview.FailedToReceiveAd += adview_FailedToReceiveAd;
          adview.ReceivedAd += adview_ReceivedAd;
          adview.LoadAd(request);
      }
  
      void adview_ReceivedAd(object sender, AdEventArgs e)
      {
          Debug.WriteLine("get ad success");            
      }
  
      void adview_FailedToReceiveAd(object sender, AdErrorEventArgs e)
      {
          Debug.WriteLine("get ad failed: " + e.ErrorCode);
      }
  
  }
  </code></pre>