locked
What will happen if I don't call shutdown() of some MF interfaces? RRS feed

  • Question

  • Hi,

    As you know, we need to call shutdown() member function of some MF interfaces (ex: IMFMediaEventQueue) when we don't use those objects any more. But for each MF object, we already need to call add_ref() before using it and call release() after the job is done. It's now more error prone that we need to call shutdown() if we don't need that object. CComPtr won't help this time because it only calls release() in destructor. So I want to know what will happen if shutdown() is not called. If it's still required, maybe it's show time for Loki::SmartPtr.

    Monday, December 11, 2006 2:26 PM

Answers

  • Sure, it's true that some implementations of these interfaces will not have any circular references, and therefore Shutdown() on them will be a no-op. 

    However, it's easy to imagine an implementation in which e.g. object1 and object2 each have pointers to the other, and they need to know that these pointers are valid, so each of them has addrefed the pointer to the other.  Shutting down object1 is the only way to release that last reference to object2, which in turn is the only way to get object2's reference on object1 released so object1 can get deleted.

    So to be on the safe side, always make sure you're shutting down any objects that have a Shutdown() method, even though smart pointers don't do this automatically.  You'll avoid memory leaks.

    Wednesday, December 13, 2006 6:05 PM

All replies

  • I saw the description about IMFMediaSink::ShutDown, it says "Inside this method, the media sink should break any circular reference counts."

    I checked the implementation of shutdown in several MF samples. It seems that the way to "break cicular reference counts" is to release the interfaces held in IMFMediaSink. Since IMFMediaSink holds nothing any more, there aren't cicular reference counts any more and IMFMediaSink will be released correctly later.

    However, the smart ponter can't work gracefully here. The smart pointer release resource in the destructor. But in MF, you need to release certain resources in ShutDown( ).

    Wednesday, December 13, 2006 4:09 AM
  • Sure, it's true that some implementations of these interfaces will not have any circular references, and therefore Shutdown() on them will be a no-op. 

    However, it's easy to imagine an implementation in which e.g. object1 and object2 each have pointers to the other, and they need to know that these pointers are valid, so each of them has addrefed the pointer to the other.  Shutting down object1 is the only way to release that last reference to object2, which in turn is the only way to get object2's reference on object1 released so object1 can get deleted.

    So to be on the safe side, always make sure you're shutting down any objects that have a Shutdown() method, even though smart pointers don't do this automatically.  You'll avoid memory leaks.

    Wednesday, December 13, 2006 6:05 PM
  • Is it possible to provide a solution like boost::weak_ptr ? I'm used to smart pointers to totally avoid resource leak and achieve exceptional safety. It's frustrating that I need to release resources manually.
    Thursday, December 14, 2006 4:42 AM