简体   繁体   English

释放*时C ++内存泄漏?

[英]C++ Memory leak when freeing *?

I am using this in a void: 我在空白中使用此:

unsigned char *nEncodedBytes = NULL;
nEncodedBytes = new unsigned char[m_lLenCompressedBytes];

short *shrtDecoded = NULL;
shrtDecoded = new short[iCountDecodedShorts];

short *m_ShortOut;
m_ShortOut = (short*)malloc(( 960*6)*sizeof(short));

unsigned char *m_DataBytes;
m_DataBytes = (unsigned char*)calloc((960*6),sizeof(char));

When I am done, I free the memory using 完成后,我使用释放内存

delete (nEncodedBytes);
delete (shrtDecoded);

    free(m_DataBytes);
    free(m_ShortOut);

Is this fine? 这样好吗 I am unsure why I was using delete in one place, and free in the other. 我不确定为什么在一个地方使用delete,而在另一个地方使用free。 I copied my code around. 我复制了我的代码。

Is there a memory leak? 有内存泄漏吗?

Thank you. 谢谢。

You use free when you use malloc . 使用malloc时可以使用free In all likelihood, you shouldn't be using malloc at all in C++; 在所有情况下,您都不应该在C ++中使用malloc it's the C way of doing things and only rarely wanted in C++. 这是C的处理方式,在C ++中很少使用。

You use delete when you allocate with new . 使用new分配时,可以使用delete new calls the constructor as well as allocating memory, and delete calls the destructor as well as releasing memory. new调用构造函数以及分配内存, delete调用析构函数以及释放内存。 These are thus the object orientated C++ options. 因此,这些是面向对象的C ++选项。 There is, however, a wrinkle. 但是,有皱纹。 Because the implementation of C++ doesn't know whether a pointer refers to an array or a single object if you allocate an array (eg nEncodedBytes = new unsigned char[m_lLenCompressedBytes]; ) then you should use delete[] instead of delete to release it. 因为C ++的实现不知道如果分配数组时指针是指向数组还是指向单个对象(例如nEncodedBytes = new unsigned char[m_lLenCompressedBytes]; ),则应使用delete[]而不是delete来释放它。

Mind you, failing to call delete[] only means that you will only call the destructor for the first object in an array so, in this particular case there should be no difference in the outcome between calling delete[] and calling delete because char has no destructor. 请注意,不调用delete[]仅意味着您将仅调用数组中第一个对象的析构函数,因此, 在这种特殊情况下 ,调用delete[]与调用delete之间的结果应该没有区别,因为char具有没有破坏者。

I don't see a memory leak in your code but since you haven't posted all your code we can't tell. 我看不到您的代码中发生内存泄漏,但是由于您尚未发布所有代码,因此我们无法告知。

You should use 你应该用

delete [] nEncodedBytes;
delete [] shrtDecoded;

as you are deleting arrays. 当您删除数组时。

Don't mix malloc and new (first because of stylistic reasons, and then because you should never delete a malloc -ed memory or free a new -ed zone). 不要将mallocnew混合使用(首先是出于样式原因,然后是因为您永远不应该deletemalloc分配的内存或free new ed区域)。 Consider using standard C++ containers . 考虑使用标准C ++容器 You won't even need to explicitly allocate memory (the library will do that for you). 您甚至不需要显式分配内存(库将为您完成此操作)。 You could code 你可以编码

std::vector<char> nEncodedBytes;
nEncodedBytes.resize(encodedbyteslen);

On Linux, use valgrind to hunt memory leaks. 在Linux上,使用valgrind搜索内存泄漏。 BTW, you might be interested by Boehm's GC , perhaps using its allocator like here . 顺便说一句,您可能会对Boehm的GC感兴趣,也许使用了这里的分配器。

BTW, when using yourself malloc you should always test its result, at least like 顺便说一句,使用自己的malloc您应该始终测试其结果,至少像

 SomeType* ptr = malloc(sizeof(SomeType));
 if (!ptr) { perror("malloc SomeType"); exit(EXIT_FAILURE); };

remember that malloc could fail. 请记住, malloc可能会失败。 You may want to limit the memory available (eg with ulimit -m in bash in your terminal) for testing purposes (eg to make malloc -or new - fail more easily to ensure you handle that kind of failure well enough). 您可能需要限制可用内存(例如使用ulimit -mbash在你的终端)用于测试目的(如做malloc -或new -失败更容易,以确保你处理那种失败的不够好)。

Is this fine? 这样好吗

Not quite. 不完全的。 There are a couple problems. 有几个问题。 One -- your troublesome use of free -- we'll address in a moment. 一个-您对free使用的麻烦-我们将在稍后解决。 But first, your use of new[] with delete (non- [] ) invokes Undefined Behavior: 但是首先,将new[]delete (non- [] )一起使用会调用未定义的行为:

Here you are using new [] : 在这里,您使用的是new []

nEncodedBytes = new unsigned char[m_lLenCompressedBytes];

And here you are using delete : 在这里,您使用的是delete

delete (nEncodedBytes);

This should be: 应该是:

delete [] nEncodedBytes;

Using the [] for of new with the non- [] form of delete invokes Undefined Behavior. new[]用于非[]形式的delete调用未定义行为。 Now in reality, all the compilers & platforms I'm aware of will handle this fine and do what you expect in this particular case -- but you should never rely on Undefined Behavior. 现在,实际上,我所知道的所有编译器和平台都可以很好地处理这种情况,并在特定情况下可以达到预期的效果但是您永远不应依赖未定义的行为。

Now for your use of malloc and free : 现在供您使用mallocfree

I am unsure why I was using delete in one place, and free in the other. 我不确定为什么在一个地方使用delete,而在另一个地方使用free。

You probably shouldn't be. 你可能不应该。 Here's a rule of thumb: 这是一个经验法则:

In C++, always use new and delete ; 在C ++中,请始终使用newdelete ; never use malloc and free . 永远不要使用mallocfree

Yes, there are exceptions, but they are first of all rare, and second of all you will know exactly when the exceptions come in to play. 是的,有例外,但首先是罕见的,其次您将确切知道例外何时开始播放。 In the example you've posted here, I see no reason that compels the use of malloc and free . 在您在此处发布的示例中,我认为没有理由强迫使用mallocfree Hence, you should not be. 因此,您不应该如此。

Contrary to (popular?) belief, mixing new / delete and malloc / free in a single C++ program does no in itself invoke Undefined Behavior or make your program otherwise ill-formed. 与流行的看法相反,在单个C ++程序中混合new / deletemalloc / free本身并不会调用Undefined Behavior或使您的程序格式错误。 You can do it, if you do it properly. 如果做得正确,就可以做到。 But you still shouldn't. 但是你还是不应该。

Is there a memory leak? 有内存泄漏吗?

Well, since you have invoked Undefined Behavior, there could be. 好吧,因为您已经调用了Undefined Behavior,所以可能会发生。 Undefined Behavior means anything can happen . 未定义的行为意味着任何事情都可能发生 You are however de-allocating everything you allocate in the code shown. 但是,您正在取消分配在显示的代码中分配的所有内容。 So aside from the UB, I see no memory leak here. 因此,除了UB,我在这里看不到任何内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM