Search code examples
c#compinvokedllimport

COM dll is not being flushed out


I am interacting with a custom COM component called CSCCOM in my c# project.

I am wrapping it with IDisposable as below:

Form1.cs

try {
    using (CSCCOMWRAP CSC = new CSCCOMWRAP()) {
        CSCCodeList CSCL = new CSCCodeList(CSC);

        comboBox1.DataSource = CSCL.List;

        Marshal.ReleaseComObject(CSCL);
    }
}
catch (COMException ex) { }

CSCCodeList.cs

try {
    var cscl = CSC.GetCodes();

    for (int i = 1; i <= cscl.Count(); i++) {
        object item = i;
        var code = cscl.Item(ref item);

        List.Add(new CSCCode((string)code.Name, Convert.ToString(code.Code)));
    }
}
catch (Exception ex) { );

Once the program is executed, I still see CSCCOM.dll twice in the ProcessExplorer's lower pane's DLL view.

This suggests that for some reason my COM dll is not getting flushed out of the system.


Solution

  • Long time no COM, but it seems to me you are never releasing the cscl or code vars in

                    var cscl = CSC.GetCodes();
    
                    for (int i = 1; i <= cscl.Count(); i++) {
                        object item = i;
                        var code = cscl.Item(ref item);
    
                        List.Add(new CSCCode((string)code.Name, Convert.ToString(code.Code)));
    

    with ReleaseComObject, resulting in RCW counts not being decremented and making the dll "float around"