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.

Comments

  1. 21 Feb 2003 at 03:52
    Great tutorial.This will definitely change the way i look up on these Matters..
  2. 09 Feb 2003 at 05:05

    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:

    Code:
    Private Sub UserControl_Initialize()


    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.

  3. 20 Jan 2003 at 12:06

    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.

  4. 20 Dec 2002 at 12:50
    Glad it helped.
  5. 10 Dec 2002 at 06:47

    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

  6. 10 Oct 2002 at 17:44
    very interesting and usefull trick. (I should have thought about it myself)
  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Circular Referencing to COM Objects.

Leave a comment

Sign in or Join us (it's free).

Jose Pablo Ramirez Vargas

Related discussion

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...

We'd love to hear what you think! Submit ideas or give us feedback