# CComPtr and IDirect3DTexture9

This topic is 4437 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do you asign a (ATL)CComPtr to a new chunk of memory? I had this: LPDIRECT3DTEXTURE9* m_pTextures; m_pTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials]; and I want to do this: ATL::CComPtr<IDirect3DTexture9> m_pTextures; m_pTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials]; Thanks

##### Share on other sites
What you're doing in your code is assigning a CComPtr to an array of pointers. This is incorrect and I don't think it will even compile. I suppose the simplest way to do this is to create an array of safe pointers. So your code would be more like this:

ATL::CComPtr<IDirect3DTexture9> *m_pTextures;
m_pTextures = new ATL::CComPtr<IDirect3DTexture9>[m_dwNumMaterials];

Then, you'll need to assign each array element as you would a single CComPtr. Don't forget that you'll need to delete[] the array when you're done. Otherwise, you'll get no benefit from the safe pointers at all.

I don't know about ATL, but the boost library provides a nice safe pointer implementation for arrays as well. It would go something like this:

boost::safe_array< ATL::CComPtr<IDirect3DTexture9> > m_pTextures( new ATL::CComPtr<IDirect3DTexture9>[m_dwNumMaterials] );

That way, your array is cleaned up without having to deal with the delete[] operator. You might want to typedef some of those templated types since that line is pretty unwieldy to type. =)

Hope this helps a bit!

##### Share on other sites
Thanks, I'll try it out and let you know if it worked.

##### Share on other sites
It doesn't work, I get the error:
meshobject.cpp(61) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'ATL::CComPtr<T> *' (or there is no acceptable conversion)        with        [            T=IDirect3DTexture9        ]        d:\program files\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h(283): could be 'T *ATL::CComPtr<T>::operator =<IDirect3DTexture9>(const ATL::CComPtr<T> &) throw()'        with        [            T=IDirect3DTexture9        ]        d:\program files\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h(274): or 'IDirect3DTexture9 *ATL::CComPtr<T>::operator =(T *) throw()'        with        [            T=IDirect3DTexture9        ]        d:\program files\microsoft visual studio 8\vc\atlmfc\include\atlcomcli.h(291): or 'IDirect3DTexture9 *ATL::CComPtr<T>::operator =(const ATL::CComPtr<T> &) throw()'        with        [            T=IDirect3DTexture9        ]        while trying to match the argument list '(ATL::CComPtr<T>, ATL::CComPtr<T> *)'        with        [            T=IDirect3DTexture9        ]

##### Share on other sites
That's kind of odd. Did you forget the * in the first line? It should be:
ATL::CComPtr<IDirect3DTexture9> *m_pTextures;

and not:
ATL::CComPtr<IDirect3DTexture9> m_pTextures;

I believe that would cause the error you see. I wrote some test code real quick and this compiles just fine. It's basically the operations you're attempting but with a fake class. The int assignment would be the interface and Blah is CComPtr in it's most basic form:

template <typename T>class Blah {public:	Blah() throw() {}		void operator =(const T &val) {}};void foo() {	Blah<int> *testArr;	testArr = new Blah<int>[30];		for(int i = 0; i < 30; i++) {		testArr = 50;	}		delete [] testArr;}

If you're still having trouble, please post your code and I'll take a look and see what might me causing the error.

##### Share on other sites
It was as you said. I didn't noticed you changed that line too. However I decided to dump C++ in favor of C#. There where just to many issues to fix out.

Anyway thanks for you help!

• 40
• 15
• 10
• 23
• 19