[英]Array of CString -> Memory Leak?
I have an array of CStrings as a member in my class: 我的班级中有一组CStrings作为成员:
...
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). 尽管正确调用了该类的析构函数,但我的分析工具告诉我,这七个mString中的每一个都有内存泄漏(每次我分配和取消分配我的类时)。
I thought that CStrings manage their memory themselves. 我以为CString可以自己管理内存。 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 new
or new[]
(or malloc
) and don't free (with delete
, delete[]
or free
) the memory. 除非您使用new
或new[]
(或malloc
)进行分配并且不释放(使用delete
, delete[]
或free
)内存,否则您不会泄漏任何内容。
In your case, the array is in automatic storage, so it automatically gets cleaned up. 在您的情况下,阵列处于自动存储中,因此会自动进行清理。 Also, CString
s automatically manage their memory. 另外, CString
自动管理其内存。 Either it's a false positive, or there's some other code causing the issue. 要么是误报,要么是其他一些代码导致了此问题。
Edit - Although a CString
doesn't leak, a false positive might not be the case. 编辑-尽管CString
不会泄漏,但肯定不是假阳性。 Are you my any chance initializing as: 您是否有机会初始化为:
unsigned char* x = new unsigned char[10];
CString str(x);
and forgetting to delete[] x
, or something similar? 忘记了delete[] x
或类似的东西?
Edit 2 - Maybe the error comes for un-deleted instances of your class: 编辑2-也许错误是由于您的类的未删除实例引起的:
struct X
{
private:
CString mStrings[7];
};
//...
X* x = new X;
I am going to make a leap of faith here that CString
is in fact a char *
. 在这里,我要相信CString
实际上是char *
。
If this is the cast and the class has allocated them, then the destructor needs to deallocate them. 如果这是强制类型转换并且该类已经分配了它们,则析构函数需要释放它们。
A delete of that object will be a shallow one. 删除该对象将是一个浅表。 There is the problem. 有问题。
But on a final note - what is the type of CString
? 但是最后一点CString
是什么类型?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.