Jun 17, 2009

why UnAdvise ?

If you are familier with COM, you would usually work with AdviseFooEventSink / UnAdviseFooEventSink. You have to call UnAdviseFooEventSink explicitly somewhere ...... it`s better not in the scope of this event sink, especially in the destructor of event object. But WHY ?

I try to demo something by directshow recently. When working with filter, I made some mistake in accident today. I did something like UnAdviseFooEventSink in the destrucotr of event object. Let`s check what happen :

  1. when the event sink object is created, the reference count should be 1.
  2. when Foo Advise the sink object, reference count of sink object increase to 2.
  3. when I don`t need the sink object anymore and try to release it, reference count decrease to 1.
  4. Now the owner of the sink object is Foo, and I wish sink object to be UnAdvise when being deleted.
  5. Since Foo is still the owner of sink object, sink object won`t be deleted if Foo doesn`t do extra work.

Thanks for the debugging function of baseclass of dshow, I got some ugly assert and try to fix it.

BTW, the same isuue would happen in cocoa of Mac, too. If 2 objects are the owner of each other (retain each other), they should be disconnected before release the final reference outside of the scope.

No comments: