What are the three methods in IUnknown interface and their use?

Showing Answers 1 - 1 of 1 Answers

ferpferp

  • Mar 21st, 2006
 

COM uses a method of "reference counting" to keep track of when the resources taken up by the object can be physically released. Basically it works like this. A object instance is created in a program and a reference is held against it. The reference count is 1. If another reference is made to the same instance of the object, the reference count is incremented by 1 so it is now 2. If the first reference is set to nothing, the reference count is decremented but as far as COM is concerened the object is still alive as the reference count is not 0. When the last reference is set to nothing, the count is decremenetd to 0 and COM can release the resources used by the object back to the system.

As a result of the above, all COM classes implement these 3 methods:-

  • QueryInterface
  • AddRef
  • Release

The QueryInterface method is used by COM to query whether the component supports a particular interface (so it is passed an Interface ID). If it does support it, a pointer is returned to IUnknown otherwise NOTSUPPORTED value is returned (can't remember the exact name)

AddRef is responsible for the increment of the reference count.

Release is responsible for the decrement of the reference count.

Thus, circular references are a real problem within COM and Garbage collection employed under .Net is one of the ways around this problem.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions