title: Delphi 2009 - Can an Interface Property Cause a Memory Leak?
body: <p>I inherited an Intraweb app that had a 2MB text file of memory leaks as reported by FastMM4.  I've got it down to 115 instances of one class leaking 52 bytes.</p>
  
  <p>A brief description of the bad actor is:  </p>
  
  <pre><code>TCwcBasicAdapter = class(TCwcCustomAdapter)  
    protected  
      FNavTitleField: TField;  
      function GetAdapterNav(aDataSet: TDataSet): ICwcCDSAdapterNav; override;  
    public  
      constructor Create(aDataSource: TDataSource; aKeyField, aNavTitleField: TField; aMultiple: boolean);  
    end;  
  </code></pre>
  
  <p>and the interface is:  </p>
  
  <pre><code>  ICwcCDSAdapterNav = interface(IInterface)  
  </code></pre>
  
  <p>Am I barking up the wrong tree, since the property is reference counted? Are there any circumstances where the interface property could keep the class from being destroyed?</p>
  
  <p>Here is the implementation of the method above:</p>
  
  <pre><code>function TCwcBasicAdapter.GetAdapterNav(aDataSet: TDataSet): ICwcCDSAdapterNav;
  var
    AdapterNav: TCwcCDSAdapterNavBase;
  begin
    result := nil;
    if Assigned(aDataSet) then begin
      AdapterNav := TCwcCDSAdapterNavBasic.Create(aDataSet, FKeyField.Index, FNavTitleField.Index);
      try
        AdapterNav.GetInterface(ICwcCDSAdapterNav, result);
      except
        FreeAndNil(AdapterNav);
        raise;
      end;
    end;
  end;
  </code></pre>
  
  <p>with the class declared as:</p>
  
  <pre><code>TCwcCDSAdapterNavBase = class(TInterfacedObject, ICwcCDSAdapterNav)
  </code></pre>