   Pointer problem driving me mad?
  I have a struct for a scrolling background. I'm calling a function to create and initialize a new background and store it in an std::vector called m_Slides. When i create one background, all is well. When i create a second one, i get a violation access error whenever i try to use the Texture variable of the first Slide which is an LPDIRECT3DTEXTURE.<EDIT>Further investigation showed that whenever i create a new Slide, the Texture values (ie: Width, Height) of the first one become gibberish</EDIT>Here's the code:The function calls:PushBackground("Grass.png", D3DXVECTOR2(1.0f, 0.0f), TILE_HOR);PushBackground("Trees.png", D3DXVECTOR2(1.0f, 0.0f), TILE_HOR);The struct:struct Slide{LPDIRECT3DTEXTURE9 Texture;std::vector<PreTransformedTextured> Vertices;D3DXVECTOR2 Velocity;unsigned char Tileable;Slide() : Texture(NULL), Tileable(0x00) {}~Slide() { SAFE_RELEASE(Texture); }};The function itself://=====================================================================================================// PushBackground(): Adds a new scrolling background in increasing distance from the camera.// The third parameter can be: TILE_VER and/or TILE_HOR or TILE_NONE where the// background is tiled either vertically, horizontolly or isn't tiled at all//=====================================================================================================bool CFramework::PushBackground(LPCSTR TextureName, const D3DXVECTOR2& Velocity, UCHAR Tileable){Slide* Temp = new Slide;HRESULT hResult = D3DXCreateTextureFromFileA(m_d3ddev, TextureName, &Temp->Texture);if (FAILED(hResult)){SHOW_ERROR(hResult)return false;}D3DSURFACE_DESC Desc;Temp->Texture->GetLevelDesc(0, &Desc);float ScreenWidth = (float)m_ScreenWidth;float ScreenHeight = (float)m_ScreenHeight;float QuadWidth = 0.0f;float QuadHeight = 0.0f;if (Tileable == TILE_NONE){QuadWidth = (float)Desc.Width;QuadHeight = (float)Desc.Height;}else if (Tileable == TILE_HOR){QuadWidth = ScreenWidth;QuadHeight = (float)Desc.Height;}else if (Tileable == TILE_VER){QuadWidth = (float)Desc.Width;QuadHeight = ScreenHeight;}else if (Tileable == (TILE_VER | TILE_HOR)){QuadWidth = ScreenWidth;QuadHeight = ScreenHeight;}elsereturn false;PreTransformedTextured* Verts = new PreTransformedTextured[6];float U = QuadWidth / Desc.Width;float V = QuadHeight / Desc.Height;// To position the Quad at the Bottom Left corner, not the top left.float yOffset = (ScreenHeight - QuadHeight > 0.0f) ? ScreenHeight - QuadHeight : 0.0f;QuadHeight += yOffset;static float zDepth = 0.0f;float rhw = 1.0f;// First TriangleVerts[0] = PreTransformedTextured(0.0f, yOffset, zDepth, rhw, 0.0f, 0.0f);Verts[1] = PreTransformedTextured(QuadWidth, yOffset, zDepth, rhw, U, 0.0f);Verts[2] = PreTransformedTextured(QuadWidth, QuadHeight, zDepth, rhw, U, V);// Second TriangleVerts[3] = PreTransformedTextured(QuadWidth, QuadHeight, zDepth, rhw, U, V);Verts[4] = PreTransformedTextured(0.0f, QuadHeight, zDepth, rhw, 0.0f, V);Verts[5] = PreTransformedTextured(0.0f, yOffset, zDepth, rhw, 0.0f, 0.0f);Temp->Vertices.insert(Temp->Vertices.begin(), Verts, &Verts[5] + 1);Temp->Tileable = Tileable;Temp->Velocity = Velocity;m_Slides.push_back(*Temp);--zDepth;Temp->Texture->AddRef();delete Temp;delete [] Verts;return true;}
  There are two obvious things I can see wrong at the moment. The first one is 'minor', the second is what's causing your problem.1. You instantiate a new Slide called Temp - but if the D3D fails to load the texture, you return out of the function and don't deallocate this, this will lead to a memory leak.2. You are assinging the pointer to your object Temp to the std::vector. But then you delete the object Temp at the end of the function - this means that the pointer stored in the Vector will point to an object that no longer exists.   ;  Thanks for the first one but the second is wrong.. I'm placing the object pointed to by Temp in the vector. Temp is a pointer not an object.   ;  m_Slides.push_back(*Temp);That line adds the pointer to Temp into the Vector.   ;  No, that lines adds the value pointed to by Temp to m_Slidesm_Slides is defined as : std::vector<Slide> m_Slides;   ;  My apolgies - I read the * on the wrong side of the word Temp, however Temp is of type Slide* - so Temp points to a Slide object - the Slide object is instantiated at the top of the function - as soon as you exit the function - that object is no longer within scope. So either way - what goes onto the std::vector is garbage once the function exits.   ;  You need to review the Rule of Three for C++ classes. You define a constructor and destructor, but not a copy constructor.When the Slide structure is copied (which is done many times by your code), you don't transfer the data from the old structure to the new. In addition, during that copy, the old structure is deleted and you release the texture.I'd suggest you change your m_Slides vector to vector<Slide*>.If you want to see what's happening, add OutputDebugString(..) messages to the Slide constructor and destructor and step through your code.FYI: That is, when you push a Slide into m_Slides, the vector resizes. When it does that, it instantiates new members in new memory spaces, copies the old members to the new ones in the new memory space and deletes the old members.   ;  First off thanks for your help so far :)That's legal, otherwise creating one background won't work.I'm allocating memory for an object, placing that object in a vector then deleting the pointer to that object..If what you said was true, then this wouldn't compile, but it does:#include <iostream>#include <vector>using namespace std;void func(vector<int>& v){int* p = new int;*p = 5;v.push_back(*p);delete p;}  int main(){vector<int> foo;func(foo);cout << foo[0] << endl;};  Buckeye, that makes sense.. *looks embarrassed*I'll fix the code :)   ;  No need to be embarassed. I'd be surprised to hear from any programmer that hasn't done that.