2

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?

5
  • The getter functions return a pointer reference, and the variables are pointer references, so they represent the pointers within the Mesh class. At least that is my understanding which may be wrong. Commented Jul 10, 2012 at 5:42
  • 1
    Just got it wrong too. Using get to return a reference to a pointer and then reassigning that pointer is hardly readable. IMHO the get... methods should just return pointers and for setting the variables you should write setters instead. Otherwise you could just make those variables public (your current implementation is rather 'object obfuscation' than 'object oriented'). Commented Jul 10, 2012 at 5:46
  • This is cut down use case code, my real code uses interfaces and much more methods and protections. Commented Jul 10, 2012 at 5:48
  • did you mean pMaterials = new D3DMATERIAL9[matCount]; pTextures = new LPDIRECT3DTEXTURE9[texCount]; ? Commented Jul 10, 2012 at 6:02
  • oups, yes I did. Added to the post Commented Jul 10, 2012 at 6:03

4 Answers 4

3

I wrote the memory leaks check in cppcheck. and I agree that is a false positive. I will fix it.

feel free to report it in the cppcheck issue tracker.

Sign up to request clarification or add additional context in comments.

1 Comment

1

It might be possible that you are not leaking memory in case you call that loading function only once. Anyway, your class design looks quite fragile as there is no proper data encapsulation for those pointers. It would be better if you had setters for the data that gets loaded in the loading function. This would allow the class owning the data to delete old data in case setters are called more than once.

Comments

1

As long as you destroy your Mesh object at some point you won't be leaking your two arrays.

It is not uncommon for static analysis tools to have false positives, especially when checking for run-time issues such as memory leaks. Try using a run-time analysis tool such as Valgrind if you're interested in memory leaks.

Comments

1

No, it seems that your code is ok.

Valgrind says "invalid delete/free/realloc", but the cause of it is probably that you (seem to) free ther pointer that you have never allocated.

Since you are doing some funny things with the pointers here, you probably have a false positive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.