Skip to Content
0
Aug 22, 2023 at 08:43 AM

Best practices for cleanup of COM objects / UI API / DI API SDK objects

103 Views Last edit Aug 22, 2023 at 08:56 AM 5 rev

In my addon, I am handling the SBOApplication's AppEvent like this:

//AppEvent handler code:

private void SboApplication_AppEvent(SAPbouiCOM.BoAppEventTypes EventType)

{

if (EventType == SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged ||

EventType == SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition ||

EventType == SAPbouiCOM.BoAppEventTypes.aet_ShutDown)

{

SetStatusBarText(

"Disconnecting add-on from company",

SAPbouiCOM.BoMessageTime.bmt_Short,

SAPbouiCOM.BoStatusBarMessageType.smt_None);

oCompany.Disconnect();

System.Windows.Forms.Application.Exit();

SetStatusBarText(

"Exited add-on application",

SAPbouiCOM.BoMessageTime.bmt_Short,

SAPbouiCOM.BoStatusBarMessageType.smt_Success);

}

}

private void SetStatusBarText(string text, SAPbouiCOM.BoMessageTime messageTime,

SAPbouiCOM.BoStatusBarMessageType messageType)

{

oSboApplication.StatusBar.SetText(text, messageTime, messageType);

}

Other than this, in the event handler for ItemEvent and other event handlers, do I need to take care of any cleanup of COM objects, to avoid memory leakage? What are the best practices related to COM objects in event handling code?

Do I need to call System.GC.Collect(); in each event handler function?

Do I need to write each event handler in try-catch block and explicitly set each SDK object reference to null in the finally block?