Library tutorials & articles
Circular Referencing to COM Objects
Introduction
As many of you may know, every time you use a object variable to reference an object, the COM reference count is incremented for that object, and that is something you can learn how to avoid in this tutorial. For those of you who don't know what I am talking about, let me explain.
Every COM object, such as a VB form, or a textbox, or an instance object of a VB class, or an instance object of a C++ class, etc., implements the IUnknown interface. An interface can be considered a personality of an object. All COM objects implements the IUnknown personality. This interface provides access to other interfaces and also keeps count of how many object variables are pointing to the object. Why this? Well, a COM object will not be unloaded from memory if there is a reference to it (general rule, but some other rules may apply, like Visibility). This is why is a good practice to always set object variables to Nothing while programming in Visual Basic: To make VB reduce the reference count.
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.