views:

42

answers:

2

Guys,

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.

I would really appreciate your help on this one.

-- Ruby

+3  A: 

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"

MrDosu
How did I missed that? Thanks a lot for pointing it out :) Really appreciate it :)
Ruby
because COM is sadistic black magic created to torture the progging kind ;p
MrDosu
A: 

Here is what needed to be modified in 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)));

        Marshal.ReleaseComObject(code);
    }

    Marshal.ReleaseComObject(cscl);
}

Hope it helps someone!

Ruby