none
Can we use COM object as reference parameter. Will it cause item leak? RRS feed

Answers

  • Hello,

    You can pass a COM object as a parameter. This doesn't increment the reference counter. Still, you need to determine who - the caller or the callee - is reponsible for releasing it. You may want to read When to release COM objects in Office add-ins developed in .NET.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    • Proposed as answer by Jie BaoModerator Thursday, February 23, 2012 8:20 AM
    • Marked as answer by LoloPolo Thursday, February 23, 2012 10:12 AM
    Wednesday, February 22, 2012 12:45 PM
  • Andrei already responded - no, it won't. Reference will be to RCW, not to real underlying COM object, so reference count will not inrease on that COM object.
    • Marked as answer by LoloPolo Thursday, February 23, 2012 9:46 AM
    • Unmarked as answer by LoloPolo Thursday, February 23, 2012 9:47 AM
    • Marked as answer by LoloPolo Thursday, February 23, 2012 10:12 AM
    Thursday, February 23, 2012 8:36 AM

All replies

  • Hello,

    You can pass a COM object as a parameter. This doesn't increment the reference counter. Still, you need to determine who - the caller or the callee - is reponsible for releasing it. You may want to read When to release COM objects in Office add-ins developed in .NET.


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    • Proposed as answer by Jie BaoModerator Thursday, February 23, 2012 8:20 AM
    • Marked as answer by LoloPolo Thursday, February 23, 2012 10:12 AM
    Wednesday, February 22, 2012 12:45 PM
  • Can we use COM object as reference parameter. Will it cause item leak?
    Thursday, February 23, 2012 8:23 AM
  • I'm sorry, can you provide an example of doing this?

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Thursday, February 23, 2012 8:30 AM
  • Andrei already responded - no, it won't. Reference will be to RCW, not to real underlying COM object, so reference count will not inrease on that COM object.
    • Marked as answer by LoloPolo Thursday, February 23, 2012 9:46 AM
    • Unmarked as answer by LoloPolo Thursday, February 23, 2012 9:47 AM
    • Marked as answer by LoloPolo Thursday, February 23, 2012 10:12 AM
    Thursday, February 23, 2012 8:36 AM
  • Thanks Andrei and DamianD.

    Example:

     

    public void outlookApp_ItemSend(object item, ref bool cancel)

    {

                      Outlook.MeetingItem currentMeeting = (Outlook.MeetingItem)item;

     Outlook.AppointmentItem appointment = currentMeeting.GetAssociatedAppointment(true);

      Somemethod(ref  appointment ); // Call a method

    //In finally block I am releasing this object

    }

    private void Somemethod(ref Outlook.AppointmentItem objAppointment)

    {

                    // some operation on objAppointment

                    //and I am not releasing it here

    }

    Is it ok? or will it cause item leak?

    Thursday, February 23, 2012 9:58 AM
  • Yes, remember just to call Release on both meeting and appointment items.
    Thursday, February 23, 2012 10:05 AM
  • Thank you for the expplanation. This doesn't cause item leak.

    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader

    Please mark answers and useful posts to help other developers use the forums efficiently.

    Thursday, February 23, 2012 10:08 AM