title: How to retrieve object pointer from ATL collection of objects?
body: <p>I have a collection of objects that is defined as:</p>
  
  <pre><code>typedef IField                  ItemInterface;
  typedef CComObject&lt;CField&gt;*     ItemClassPtr;
  typedef CAdapt&lt; CComPtr&lt;ItemInterface&gt; &gt; ItemType;
  typedef std::vector&lt; ItemType &gt; ContainerType;
  </code></pre>
  
  <p>and I have created several of the CField objects via a series of calls (ignoring hresult):</p>
  
  <pre><code>IField* ppField = 0;
  hresult = CField::CreateInstance(&amp;ppField);
  ItemType spField = ppField;
  m_coll.push_back(spField);
  ppField-&gt;Release();
  </code></pre>
  
  <p>and now I'm trying to retrieve a pointer to an object so that I can call one of it's methods:</p>
  
  <pre><code>ItemClassPtr pField;
  short type1;
  m_coll[index].m_T-&gt;QueryInterface( __uuidof(ItemInterface), (void **)&amp;pField ) );
  pField-&gt;get_Type(&amp;type1);
  </code></pre>
  
  <p>and it crashes on access violation at the get_Type call.
  this was changed in response to a responder's post to:</p>
  
  <pre><code>short type1;
  IField * ppField = m_coll[index].m_T;
  CComQIPtr&lt;CField, &amp;__uuidof(IField)&gt; pField = ppField;
  pField-&gt;get_Type(&amp;type1);
  </code></pre>
  
  <p>but it still crashes when I attempt to trace in the get_Type call.</p>
  
  <p>Here is the preamble to the CField class definition:</p>
  
  <pre><code>class ATL_NO_VTABLE CField : 
      public CComObjectRootEx&lt;CComSingleThreadModel&gt;,
      public CComCoClass&lt;CField, &amp;CLSID_Field&gt;,
      public ISupportErrorInfo,
      public IFieldAccess,
      public IDispatchImpl&lt;IField, &amp;IID_IField, &amp;LIBID_SQLite02&gt;
  {
      friend class CFields;
      friend class CrecordSet;
  public:
      CField();
      ~CField();
  
  DECLARE_REGISTRY_RESOURCEID(IDR_FIELD)
  
  DECLARE_PROTECT_FINAL_CONSTRUCT()
  
  BEGIN_COM_MAP(CField)
      COM_INTERFACE_ENTRY(IField)
      COM_INTERFACE_ENTRY(IDispatch)
      COM_INTERFACE_ENTRY(ISupportErrorInfo)
      COM_INTERFACE_ENTRY_IID(__uuidof(IField), CField)
  END_COM_MAP()
  </code></pre>
  
  <p>Please help!</p>
  
  <p>Note that this question is an offshoot from a previous ATL question <a href="https://stackoverflow.com/questions/16524138/in-icollectiononstlimpl-implementation-cant-access-m-t-or-item-objects-member">In ICollectionOnSTLImpl implementation, can't access m_T or item object's members</a>
  That more fully describes the collection class.  I have replaced the #defines with typedefs.</p>
  
  <p>Vance</p>