简体   繁体   中英

VB6 call C# doesnt release

I have a VB6 code that call C# by using late binding, when the C# finish the VB6 doesn't release the C# reference, i tried implementing in the C# IDisposable i tried setting the the reference to nothing and it didn't work

Is it possible that the VB6 code doesn't release the ref? Is there any other way to release all reference to the C# code? Is there any annotation i might use?

To give the whole story the VB6 is third party code, i cant add functionality/code call to it.

Thanks X

VB6

Private Sub Command1_Click()
    Dim obj As Object
    Set obj = CreateObject("test1.class1")
    obj.msg
    Set obj = Nothing
End Sub

C#

namespace test1
{
    [ClassInterface(ClassInterfaceType.None)]
    public class Class1 : IDisposable
    {
        public void msg()
        {
            Console.Write("msg");
        }
        ~Class1()
        {
            Console.Write("~Class1");
        }

        public void Dispose()
        {
           Console.Write("Dispose");
        }
    }
}

This is simply not the way memory management works in managed code. The rules don't change just because you expose it as a [ComVisible] class. Your vb6 code will release the CCW (COM callable wrapper). But that just removes a reference to the C# object. The object doesn't get destroyed and the finalizer won't run until the garbage collector runs. Which in your posted snippet won't happen until the program terminates, you are not allocating enough managed objects to trigger a GC.

This is not a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM