Results 1 to 1 of 1

Thread: What is the proper way for an ActiveX object to keep count?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,201

    What is the proper way for an ActiveX object to keep count?

    I know in the simplest sense that when you use AddRef it increases the count by one, and Release decreases the count by one. I'm going to be experimenting with writing a COM/ActiveX DLL in assembly code, so I need to know some internal details about that counter, so I make sure that its counter behaves correctly (if it doesn't hold the right count, it can cause problems). Is there supposed to be one counter for the entire DLL, and every time a new object (instance of the class) is created, the counter gets is incremented? Or does it also incremented when several references to the same instance get added. For example in the VB6 code below:

    Code:
    Dim a As MyClass
    Dim b As MyClass
    Dim c As MyClass
    
    Set a = New MyClass
    Set b = New MyClass
    Set c = b
    As you can see, there are 2 instances of the class, but 3 references (1 reference to the first instance, and 2 references to the second instance). So should the internal counter for the DLL show 2 (based on total instances), or 3 (based on total references)? Or is there not even supposed to be a single counter for the whole DLL? Is it a situation where each instance gets its own counter, where the counter for the first instance will hold the number 1 (for 1 reference to the first instance), and the counter for the second instance will hold the number 2 (for 2 references to the second instance)?

    And then comes the problem of handling an object that has multiple interfaces. For example, the class might be MyClass, but this class might implement 2 interfaces such as IMyInterface1 and IMyInterface2. And the code in VB6 might look like
    Code:
    Dim a As IMyInterface1
    Dim b As IMyInterface1
    Dim c As IMyInterface2
    
    Set a = New MyClass
    Set b = New MyClass
    Set c = b
    In this case, is there one counter for each interface such that the IMyInterface1 counter will hold the number 2 (there are 2 instances of that interface being used), and the IMyInterface2 counter will hold the number 1 (there's only 1 instance of that interface being used)? Or is it still a matter of each object getting its own counter, and regardless of which interface is being used, only the number of times that the object is referenced is stored by the counter (such that the first counter will hold the number 1, as the first object is only referenced once, but the second counter will hold the number 2, as the second object is being referenced twice)?


    Also, is the calling function always responsible for calling AddRef? Or are the interface functions also supposed to call AddRef in some situations? For example, when the calling function calls QueryInterface, if it is successful at getting an interface pointer here is the calling function responsible for then also calling AddRef? Or is the interface's own QueryInterface function supposed to then call AddRef (while the calling function that originally called QueryInterface doesn't need to take any additional action)?
    Last edited by Ben321; Mar 2nd, 2025 at 12:20 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width