c# - Excel Interop Try Catch causes memory leak I find it hard to believe I have to pick between error handling or leaking memory? -
i decided try adding vba code excel workbooks dynamically, projects i'm working on @ work, around pain of memory leaks in vsto i.e. if miss double dot using excel interop. aid in prototyping business requirements in macro enabled spreadsheet.
to start began classic hello world. however, when added try catch notify users trust center settings may need changing. noticed excel not close , hang around in task manager. having double checked code double dots, excessive rubber duck debugging. noticed mere existence of try catch facilitate behaviour on office 2013 installation. can remove memory leak taking try catch, means code write in fashion have no error handling. (clearly not acceptable).
my reference sources: how clean excel interop objects?
https://support.microsoft.com/en-us/kb/303872
using system; using system.collections.generic; using system.linq; using system.text; using vbide = microsoft.vbe.interop; using excelapi = microsoft.office.interop.excel; using helpers; using system.windows; namespace outlooktoolbarautotestablecode { class helloworldmacrocreation { public void createhelloworld() { excelhelper excelhelp = new excelhelper(); //create excel// excelapi._application excelapp; //create workbook// excelapi.workbooks workingworkbooks; excelapi.workbook workingworkbook; vbide.vbcomponent workingmodule; //vbide.vbe visualbasicextension; excelapi.sheets workingworksheets; excelapi.worksheet workingworksheet; vbide.vbprojects workingprojects; vbide.vbproject workingproject; vbide.vbcomponents workingcomponents; vbide.vbcomponent workingcomponent; vbide.codemodule workingcodemodule; try { excelapp = new excelapi.application(); excelapp.visible = true; //workingworkbooks = excelapp.workbooks; //workingworkbooks.add(); //workingworkbooks = null; //workingworkbooks = excelapp.workbooks; //workingworkbook = workingworkbooks.get_item(1); //workingworksheets = workingworkbook.worksheets; //workingworksheet = excelapp.activesheet; messagebox.show("hello"); //visualbasicextension = excelapp.vbe; //workingprojects = visualbasicextension.vbprojects; //workingproject = workingworkbook.vbproject; //workingcomponents = workingproject.vbcomponents; //workingmodule = workingcomponents.add(vbide.vbext_componenttype.vbext_ct_stdmodule); //workingcomponent = workingcomponents.item(1); //workingcodemodule = workingmodule.codemodule; //string message = @"""hello world"""; //workingcodemodule.addfromstring("public sub helloworld() \n msgbox(" + message + ") \n" + "end sub \n"); //excelapp.run("helloworld"); //workingcodemodule = null; //workingmodule = null; //workingcomponent = null; // workingcomponents = null; //workingproject = null; //workingprojects = null; //visualbasicextension = null; workingworksheet = null; workingworksheets = null; workingworkbook = null; workingworkbooks = null; excelapp = null; workingworksheet = null; workingworksheets = null; workingworkbook = null; workingworkbooks = null; excelapp = null; gc.collect(); gc.waitforpendingfinalizers(); gc.collect(); gc.waitforpendingfinalizers(); messagebox.show("test 1"); } catch (exception ex) { workingcodemodule = null; workingmodule = null; workingcomponent = null; workingcomponents = null; workingproject = null; messagebox.show("it appear trust center settings preventing automation working, please check settings , try again."); messagebox.show(ex.message); workingworksheet = null; workingworksheets = null; workingworkbook = null; workingworkbooks = null; excelapp = null; gc.collect(); gc.waitforpendingfinalizers(); gc.collect(); gc.waitforpendingfinalizers(); return; } //create worksheet// workingworksheet = null; workingworksheets = null; workingcodemodule = null; workingmodule = null; workingcomponent = null; workingcomponents = null; workingproject = null; workingworkbook = null; workingworkbooks = null; excelapp = null; gc.collect(); gc.waitforpendingfinalizers(); gc.collect(); gc.waitforpendingfinalizers(); // messagebox.show("clean complete"); } }
}
is known issue? have missed obvious? there way can have no memory leaks , error handling? there better way track memory leak references? cannot imagine how'd track on large project. note i've commented out fair chunk of unnecessary code.
thanks in advance
tim
Comments
Post a Comment