Library tutorials & articles
Circular Referencing to COM Objects
- Introduction
- Why Avoid the Reference Count?
- 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.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
There is no secret as to how you decrease the reference count of a COM object. All you have to do is call the Release method of the IUnknown interface. But VB will hide this. However, you can cheat by creating or using a type library that does define the IUknown interface. Then you could just do something like this:
dim oObject as IUnknown
Set Splitter1.LeftCtl = LeftThing
Set Splitter1.RightCtl = RightThing
set oObject = LeftThing
oObject.Release
set oObject = Nothing
set oObject = RightThing
oObject.Release
set oObject = Nothing
End Sub
That should take care of it. However, this method is somewhat cumbersome and can be easily avoided by soft referencing. I say go with soft referencing.
I have a project with Splitter controls that require references to pairs of controls that are proportionally resized by the user.
The application GPF's (occasionally) upon termination. If run inside a browser, it causes an error report and the browser stays in memory.
I realize that setting the controls reference to Nothing during the Usercontrol_Terminate event would solve the problem, but your article explains why this was not happening. So that is good.
Your solution is intriguing, but I would like to ask you if another solution might work.
How about we reduce the refrence count on the objects in the Initialize routine? Like this:
Private Sub UserControl_Initialize()
Set Splitter1.LeftCtl = LeftThing
Set Splitter1.RightCtl = RightThing
ReduceRefrenceCountByOne LeftThing
ReduceRefrenceCountByOne RightThing
End Sub
Now the Controls will be come out of memory as we really want when the parent control terminates. Incidentally, their UserControl_Terminate events will fire correctly too.
Perhaps you have written the code to do the evil reference count manipulation?
What do you think?
G.
I struggled for a long time with my app hanging after it quits. It turned out to be a circular reference in the (approximately) 180 000 objects.
Thnx for the great explanation and work-around. Everything going smoothly again.
igitur
This thread is for discussions of Circular Referencing to COM Objects.