Do I have a memory leak? I am building a game engine and I have some code that I think is right but my code analysis tools ( cppcheck ) says I have memory leak, it maybe a false positive.
I have a ( this is a bare bones use case )
class Mesh
{
D3DMATERIAL9* mpMaterials;
LPDIRECT3DTEXTURE9* mpTextures;
D3DMATERIAL9*& GetMaterials() { return mpMaterials; }
LPDIRECT3DTEXTURE9*& GetTexures() {return mpTextures; }
};
in my mesh class I have some directx pointers When I load the mesh I send a shared_ptr to a function in my graphics manager class to be loaded.
In that function I do
void Renderer::LoadMesh( shared_ptr<Mesh> myMesh)
{
// other code
D3DMATERIAL9*& pMaterials= myMesh->GetMaterials();
LPDIRECT3DTEXTURE9*& pTextures= myMesh->GetTextures();
// other code
// and then instantiate them
pMaterials = new D3DMATERIAL9[matCount];
pTextures = new LPDIRECT3DTEXTURE9[texCount];
// And then i do some stuff with those objects.
}
Now at the end of this function is when cpp check says pMaterials and pTextures leaked their memory. It is my understanding that pMaterials and pTextures are references to the pointers within myMesh and that the memory I instantiated lives there because the pointers in the Mesh class point to that instantiated memory, and that as long as I destroy the Mesh object properly sometime later ( and call delete[] mpMaterials; delete[] mpTextures; in the Mesh destructor ) I have not leaked memory right?
pMaterials = new D3DMATERIAL9[matCount]; pTextures = new LPDIRECT3DTEXTURE9[texCount];?