none
Outlook 2007 exception - "COM object that has been separated from its underlying RCW can not be used" RRS feed

  • Question

  • I have exception when I wanna get calendar folder.
    When I get them, I use Marshal.ReleaseComObject(calFolder) but it's not working.
    Everything works for the first time, while during the next time exception is thrown.
    Should I release other object(s)??


    Outlook.MAPIFolder calFolder = application.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    calendarItems = calFolder.Items.Restrict(sFilter);



    Exception:

    COM object that has been separated from its underlying RCW can not be used.


    ************** Exception Text **************
    System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW can not be used.
       w Microsoft.Office.Interop.Outlook.ApplicationClass.get_Session()
       w AddIn.MenuForms.SendItemsForm.run() w C:\Documents and Settings\xaze\Moje dokumenty\Visual Studio 2008\Projects\AddIn\SendItemsForm.cs:wiersz 96
       w Tebo.EFORAddIn.MenuForms.SendItemsForm.send_Click(Object sender, EventArgs e) w C:\Documents and Settings\lcwik.3W\Moje dokumenty\Visual Studio 2008\Projects\Addin\MenuForms\SendItemsForm.cs:wiersz 45
       w System.Windows.Forms.Control.OnClick(EventArgs e)
       w System.Windows.Forms.Button.OnClick(EventArgs e)
       w System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       w System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       w System.Windows.Forms.Control.WndProc(Message& m)
       w System.Windows.Forms.ButtonBase.WndProc(Message& m)
       w System.Windows.Forms.Button.WndProc(Message& m)
       w System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       w System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       w System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    (...)

    Thursday, July 31, 2008 1:54 PM

Answers

  • Hello xaze,

     

    I belive you do a call Marshal.ReleaseComObject somewhere inyour code.

    This releases all references to the COM Object, not just waht's in a class/function scope.

     

    try to do object = null

    and GC.Collect() + GC.WaitForPendingFinalizers instead.

     

    Hope this helps,

    greets, Helmut

     

     

    Thursday, July 31, 2008 7:21 PM
    Answerer

All replies

  • Hello xaze,

     

    I belive you do a call Marshal.ReleaseComObject somewhere inyour code.

    This releases all references to the COM Object, not just waht's in a class/function scope.

     

    try to do object = null

    and GC.Collect() + GC.WaitForPendingFinalizers instead.

     

    Hope this helps,

    greets, Helmut

     

     

    Thursday, July 31, 2008 7:21 PM
    Answerer
  • ReleaseComObject is not the best way forward in COM based solutions unless you are in complete control and aware of the lifecycle of the objects you are working on. I would suggest as Hermet says to GC.Collect but again this is not always the best way you don't explain why you are releasing the object specifically, you also need to set the object to null as well before calling GC.Collect

     

    Regards

     

    Friday, August 1, 2008 9:49 PM
    Moderator