title: Adding interface to custom directshow transform filter
body: <p>I am working on writing custom DirectShow filter for <code>CTransformFilter</code>.
  The basic filter is working fine. I am facing issues when I add interface to the filter. The filter code that is working fine is written below:
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>interface CVideoDecoder : public CTransformFilter, public IVideoDecoderProp
  {
  public:
      static CUnknown* WINAPI
    CreateInstance(LPUNKNOWN pUnknown, HRESULT* pHresult);
  
  public:
   // Constructor
   CVideoDecoder(TCHAR*    FilterName, LPUNKNOWN pUnknown, HRESULT   *pHr);
  
   //Destructor
   ~CVideoDecoder(void);
  
   /* CTransformFilter's methods overidden by this class */
   HRESULT Transform(IMediaSample* pSourceMediaSample, IMediaSample* pDestMediaSample);
  
   HRESULT CheckInputType(const CMediaType* pInMediaType);
  
   HRESULT CheckTransform(const CMediaType* pInMediaType, const CMediaType* pOutMediaType);
  
   HRESULT GetMediaType(int Position, CMediaType* pMediaType);
  
   HRESULT DecideBufferSize(IMemAllocator* pAlloc, ALLOCATOR_PROPERTIES* pProperties);
  
   HRESULT BreakConnect(PIN_DIRECTION PinDirection);
  
   HRESULT SetMediaType(PIN_DIRECTION direction,
    const CMediaType *pmt);
  
   HRESULT allocBuffer(UWORD32 bufferSize, void** buffer);
  
   HRESULT freeBuffer(UWORD32 bufferSize, void* buffer);
   HRESULT CacheInvalidateBuffer(void* buffer, UWORD32 bufferSize);
  
   HRESULT CheckMediaType(const CMediaType *pmt);
  
  #if SUPPORT_BEGIN_FLUSH
   /* Overriding the CTransform Filter's Begin Flush */
   HRESULT BeginFlush();
  #endif
  
  #if SUPPORT_EOS
   HRESULT EndOfStream(void);
  #endif
   //HRESULT StopStreaming();
  #if QUALITY_CONTROL
   HRESULT AlterQuality(Quality q);
  #endif
  
  protected:
  private:
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The code after adding the interface looks like this:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>EXTERN_C const IID IID_IVideoDecoderProp;
  
  #if defined(__cplusplus) &amp;&amp; !defined(CINTERFACE)
  MIDL_INTERFACE("11c4bb72-1df3-4bf3-a158-f23aa886e53b")
  IVideoDecoderProp : public IUnknown
  {
  public:
      virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, __deref_out void **ppv);
      virtual ULONG STDMETHODCALLTYPE AddRef();
      virtual ULONG STDMETHODCALLTYPE Release();
      virtual HRESULT STDMETHODCALLTYPE PrintSomething() = 0;
  };
  #endif
  
  interface CVideoDecoder : public CTransformFilter, public IVideoDecoderProp
  {
  public:
  
      HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, __deref_out void **ppv);
      ULONG STDMETHODCALLTYPE AddRef();
      ULONG STDMETHODCALLTYPE Release();
      // function for testing interface implementation
      HRESULT STDMETHODCALLTYPE PrintSomething();
  
      static CUnknown* WINAPI
    CreateInstance(LPUNKNOWN pUnknown, HRESULT* pHresult);
  
  public:
   // moved to videodecoder.cpp
   // Constructor
   CVideoDecoder(TCHAR*    FilterName, LPUNKNOWN pUnknown, HRESULT   *pHr);
  
   //Destructor
   ~CVideoDecoder(void);
  
   /* CTransformFilter's methods overidden by this class */
   HRESULT Transform(IMediaSample* pSourceMediaSample, IMediaSample* pDestMediaSample);
  
  .
  .
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have implemented the interface functions (<code>QueryInterface</code>, <code>AddRef</code> and <code>Release</code>). Both, the interface and the filter class are in same header file. The problem is that after adding the interface part, the instance creation itself fails. The sequence of calls that is made when adding the filter to application is</p>
  
  <ol>
  <li>Constructor</li>
  <li>AddRef </li>
  <li>Destructor</li>
  </ol>
  
  <p>After calling the destructor, there is a crash. The same application works without the interface.</p>
  
  <p>Any suggestion on what is wrong with the filter and interface? Please let me know if I should share any more information.</p>