Paresh Suthar's Radio Weblog
And that's all I have to say about that - Forrest Gump






Subscribe to "Paresh Suthar's Radio Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

Saturday, June 12, 2004
 

Marshal.AddRefComObject anyone (take 2)?
I had the chance to take at my original posting and realized that I did it incorrectly.  Here is a modified example of how it might be used.
 
public class Test1 : IDispose
{
   private SomeClass.Interop.IFoo m_pIFoo;

   public void Test1()
   {
       m_pIFoo = (SomeClass.Interop.IFoo)new SomeClass.Interop.FooClass();
   }

   public SomeClass.Interop.IBar IBar
   {
      get
      {
       // The following line of code increments the RCW count 
       System.Runtime.InteropServices.Marshal.AddRefComObject(m_pIFoo);
       return (SomeClass.Interop.IBar)m_pIFoo;
      }
   }

   public void Dispose()
   {
      if (m_pIFoo != null)
         System.Runtime.InteropServices.Marshal.ReleaseComObject(m_pIFoo);
   }
}

public class Test2 : IDispose
{
   private SomeClass.Interop.IBar m_pIBar;

   public void Test2()
   {
       Test1 test1 = new Test1();
       m_pIBar = test1.IBar;

       // The following line of code decrements the RCW count to 1, leaving
       // m_pIBar valid!
       test1.Dispose();
   }

   public void Dispose()
   {
      if (m_pIBar != null)
         System.Runtime.InteropServices.Marshal.ReleaseComObject(m_pIBar);
   }
}
 
Also, I do appreciate the feedback!  I know that ref counting isn't the best solution, but I think it would work - especially if you think of Interop references as if they were file resources (i.e. you always clean them up deterministically).

10:13:56 AM    comment []


Click here to visit the Radio UserLand website. © Copyright 2005 Paresh Suthar.
Last update: 8/19/2005; 3:24:08 PM.
This theme is based on the SoundWaves (blue) Manila theme.
June 2004
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
May   Jul