I have an array of CStrings as a member in my class:
...
private:
CString mStrings[7];
...
Although the destructor of the class gets called correctly my analysis tool tells me that there is a memory leak in each of the seven mStrings (each time I allocate and deallocate my class).
I thought that CStrings manage their memory themselves. Does the fact that I have them in an array change anything here?
No, that shouldn’t be leaking. You don’t get any leaks unless you allocate with
newornew[](ormalloc) and don’t free (withdelete,delete[]orfree) the memory.In your case, the array is in automatic storage, so it automatically gets cleaned up. Also,
CStrings automatically manage their memory. Either it’s a false positive, or there’s some other code causing the issue.Edit – Although a
CStringdoesn’t leak, a false positive might not be the case. Are you my any chance initializing as:and forgetting to
delete[] x, or something similar?Edit 2 – Maybe the error comes for un-deleted instances of your class: