VB and Reference Counting


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: VB and Reference Counting

  1. #1
    Bobby Reid Guest

    VB and Reference Counting


    We have a VB program that is calling an ATL-based COM component. The method
    is calling is defined in its IDL file as this:

    DoWork([in] VARIANT vtWork)

    This method accepts a VARIANT so that the client can pass an integer, string,
    or object reference to it. We call this method like this in our VB code:

    Dim obj as ATLObject
    Dim work as Object
    Set obj = new ATLObject
    Set work = CreateObject("Worker.Work")
    ATLObject.DoWork obj

    In the DoWork method, the VARIANT's pdispVal field is being populated with
    the object reference for work. Based on COM rules, we were expecting that
    VB would automatically increment the reference count of this object before
    passing it; however, the ATL debugging facilities show that it's not. If
    we assign the object to a Variant and pass the variant, VB increments the
    reference count on the assignment, but decrements it again when the variant
    variable goes out of scope.

    In this situation, how can we make VB increment the reference count before
    calling the method without having VB decrement the count later?


  2. #2
    Luther Miller Guest

    Re: VB and Reference Counting

    I think when an object is instantiated, e.g., with CreateObject, it gets a
    reference count bumped up (to 1) before it gets returned. So the program
    that creates the instance doesn't need to bump it up- it is bumped up
    implicitly. However, it is that same program's responsibility to decrease
    when it is done.

    I would expect VB in tihs case not to increase after CreateObject, but to
    decrease when it gets freed later.
    If you then assign the same reference to another variable as well, e.g., an
    additional Variant, then it should get bumped again, which is what you said
    is happening.

    Sounds correct.

    If you DoWork routine fires event and needs to make sure that it does not
    released, it can artificially increase (and then later decrease) the
    reference count, I believe.

    Someone correct me if I am wrong...

    Luther


    "Bobby Reid" <bobbyr@optiosoftware.com> wrote in message
    news:39a56e3d$1@news.devx.com...
    >
    > We have a VB program that is calling an ATL-based COM component. The

    method
    > is calling is defined in its IDL file as this:
    >
    > DoWork([in] VARIANT vtWork)
    >
    > This method accepts a VARIANT so that the client can pass an integer,

    string,
    > or object reference to it. We call this method like this in our VB code:
    >
    > Dim obj as ATLObject
    > Dim work as Object
    > Set obj = new ATLObject
    > Set work = CreateObject("Worker.Work")
    > ATLObject.DoWork obj
    >
    > In the DoWork method, the VARIANT's pdispVal field is being populated with
    > the object reference for work. Based on COM rules, we were expecting that
    > VB would automatically increment the reference count of this object before
    > passing it; however, the ATL debugging facilities show that it's not. If
    > we assign the object to a Variant and pass the variant, VB increments the
    > reference count on the assignment, but decrements it again when the

    variant
    > variable goes out of scope.
    >
    > In this situation, how can we make VB increment the reference count before
    > calling the method without having VB decrement the count later?
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center