Sign in to follow this  
Aiwendil

Vector of Textures

Recommended Posts

Aiwendil    158
Hi again, I'm trying to use a std::vector to store my textures (LPDIRECT3DTEXTURE9), and I'm having some problems. The vector is declared as vector<LPDIRECT3DTEXTURE9> mTextures; I've tried:
LPDIRECT3DTEXTURE9 temp;
D3DXCreateTextureFromFile(..., &temp);
mTextures.push_back(temp);
temp->Release();
and
mTextures.resize(mTextures.size() + 1);
D3DXCreateTextureFromFile(..., &mTextures[0]);
but when I try to draw using the texture at a later time in the game, the game crashes. Is there a way to use LPDIRECT3DTEXTURE9 with vectors? Thanks

Share this post


Link to post
Share on other sites
Hodgman    51324
With your first code example, you're releasing the texture as soon as you've created it.

With your second example, you're overwriting the first entry in the vector (leaking resources) and appending a NULL pointer on the end of the vector.

Try the first one without the Release in there, or the second one changed to:
mTextures.resize(mTextures.size() + 1);
D3DXCreateTextureFromFile(..., &mTextures.back());

Share this post


Link to post
Share on other sites
iMalc    2466
I would actually store a CComPtr<IDirect3DTexture9>'s in the vector, or if necessary CAdapt<CComPtr<IDirect3DTexture9>>'s, or other such smart pointer.

Then just use push_back as normal and not have to worry about the Releasing of the object when it's removed from the container.

Share this post


Link to post
Share on other sites
Aiwendil    158
Thanks guys, it worked.

I thought that when you push something onto a vector, it's really copied into it. Is that right? If it is, why does releasing the temp texture after pushing it onto the vector release the texture in the vector as well?

Share this post


Link to post
Share on other sites
GregMichael    135
A vector is a container...it holds whatever you defined it to hold.

So for example

std::vector<Texture *> TextureList;

Would be a vector that "contains" pointers to a "Texture"

If you do the following...

Texture * texture1 = new Texture();

TextureList.push_back( texture1 );

texture1->Release();

or

delete texture1;

Then TextureList (the container) will still contain the pointer (but it will now not be valid, since you have either Released() it or deleted it...so don't try using it :)

I think that is correct.


Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by Aiwendil
I thought that when you push something onto a vector, it's really copied into it. Is that right? If it is, why does releasing the temp texture after pushing it onto the vector release the texture in the vector as well?
Tes, that's right. But LPDIRECT3DTEXTURE9 is typedef'd as IDirect3DTexture9* - I.e. it's a pointer that's stored in the object, and the pointer that's copied - not the object being pointed at.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this