Debugging 即使仍然存在引用,我是否可以销毁类实例?

Debugging 即使仍然存在引用,我是否可以销毁类实例?,debugging,vb6,activex,reference-counting,Debugging,Vb6,Activex,Reference Counting,出于调试原因,我想销毁仍然作为引用的类实例。可能吗?它不必优雅或稳定,因为这永远不会出现在生产代码中 澄清: Public Sub Main Dim o as MyClass Set o = New MyClass //o is created, one reference DestroyObject o //Class_Terminate is called and the object destroyed //Further code, not usi

出于调试原因,我想销毁仍然作为引用的类实例。可能吗?它不必优雅或稳定,因为这永远不会出现在生产代码中

澄清:

Public Sub Main
    Dim o as MyClass
    Set o = New MyClass //o is created, one reference
    DestroyObject o     //Class_Terminate is called and the object destroyed
    //Further code, not using o
End Sub                 //Possible runtime error here (don't care)

可能吗?一种方法是调用
IUnknown::Release
来手动减少引用计数,但是我现在该怎么做?我必须多久调用一次?

正如您所知,当对象认为其引用计数已为零时,它本身将调用
Class\u Terminate
,因此,您建议调用
Release
应该可以做到这一点-只要继续调用
Release
,直到
Release
本身抛出错误

建议了一种可能的方法,即有时可能获得引用计数,但我认为您不需要进入该方法,除非该方案(直到您不能
再发布
为止)不起作用


“这永远不会在生产代码中结束”-当然要小心您的假设…

正如您所知,当对象认为其引用计数为零时,它本身将调用
类\u Terminate
,因此,您建议调用
Release
应该可以做到这一点-只要继续调用
Release
,直到
Release
本身抛出错误

建议了一种可能的方法,即有时可能获得引用计数,但我认为您不需要进入该方法,除非该方案(直到您不能
再发布
为止)不起作用


“这永远不会出现在生产代码中”-当然要小心你的假设…

这是一个非常糟糕的想法

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private m_oRef As Class1

Private Sub Command1_Click()
    Dim o As Class1

    Set o = New Class1
    Set m_oRef = o
    DestroyObject o
    ' releasing m_oRef after this point will bring down the IDE '
End Sub

Private Sub DestroyObject(pArg As Object)
    Dim lRefCount       As Long
    Dim lIdx            As Long
    Dim pUnk            As IUnknown

    lIdx = ObjPtr(pArg) + &H20
    Call CopyMemory(lRefCount, ByVal lIdx, 4)
    For lIdx = 1 To lRefCount - 2
        Call CopyMemory(pUnk, pArg, 4)
        Set pUnk = Nothing
    Next
    Set pArg = Nothing
End Sub

这是一个非常糟糕的主意

Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private m_oRef As Class1

Private Sub Command1_Click()
    Dim o As Class1

    Set o = New Class1
    Set m_oRef = o
    DestroyObject o
    ' releasing m_oRef after this point will bring down the IDE '
End Sub

Private Sub DestroyObject(pArg As Object)
    Dim lRefCount       As Long
    Dim lIdx            As Long
    Dim pUnk            As IUnknown

    lIdx = ObjPtr(pArg) + &H20
    Call CopyMemory(lRefCount, ByVal lIdx, 4)
    For lIdx = 1 To lRefCount - 2
        Call CopyMemory(pUnk, pArg, 4)
        Set pUnk = Nothing
    Next
    Set pArg = Nothing
End Sub

当然,因为这不需要非常稳定,直到错误完全正常为止。您链接到的引用计数代码工作正常,即使在VB6中也是如此。坏消息是:VB编译器捕捉到要发布的调用。我想我需要另一种方法来解决这个问题。当然,因为在错误完全正常之前,这不需要是超稳定的。您链接到的引用计数代码工作得很好,即使在VB6中也是如此。坏消息是:VB编译器捕捉到要发布的调用。我想我需要另一种方法来解决这个问题。