Circular Referencing to COM Objects

Page 3 of 3
  1. Introduction
  2. Why Avoid the Reference Count?
  3. How to Soft Reference an Object

How to Soft Reference an Object

Having explained a bunch of things you probably knew already, lets show the solution.

Option Explicit

'API declaration of CopyMemory
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        pDest As Any, pSrc As Any, ByVal ByteLen As Long)

'Private variable to hold the pointer to the parent
Private mvarParent as Long

'Now you add the property code
Public Property Get Parent() as SuperListBox

dim oTemp as SuperListBox

    If (mvarParent = 0) Then
        Set Parent = Nothing
    Else
        'Copy the pointer stored in mvarParent to the temporal object variable
        CopyMemory oTemp, mvarParent, 4
        'Now set the parent property from the temporal object
        Set Parent = oTemp
        'Now clear the temporal object
        'WARNING: Failure to do this will make your program crash.
        'Why? VB will try to reduce the objects reference count to a negative number
        'because the use of CopyMemory did not increment the COM reference count.
        CopyMemory oTemp, 0&, 4
    End If
End Property

'You make the Set part of the property a Friend procedure,
'just to make sure the end programmer will not mess with it
Friend Property Set Parent(ByVal oNewParent as SuperListBox)
    mvarParent = ObjPtr(oNewParent)
End Property

The method shown here is called soft referencing. Basically, what we are doing here is tricking VB and COM by not using the provided methods for referencing an object. Instead, we get the pointer value for the object with the use of the ObjPtr() function and store it in a Long variable. Later, when we need the reference back, we use CopyMemory to make a VB-friendly version of the pointer we had stored, and then set the Parent property to equal this VB-friendly version. Of course, there is a price we pay: You will get a GPF (General Protection Failure) if you do not clear the variable yourself, so make sure you always clear the variable.

The example above uses CopyMemory again to clear the temporal variable. And just in case you are wondering, I wrote 0& in the CopyMemory call to force this zero to be a long value (4 bytes). If yo do not do it, you may get a GPF because VB may only allocate 2 bytes (Integer value) for this zero.

And finally, another way to clear the temporal object variable could be this:

Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" ( _
        Destination As Any, ByVal Length As Long)

    ZeroMemory oTemp, 4

I hope you have found this useful. If you would like to say something about this article, then please post a comment.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Weeks of coding can save you hours of planning.”