简体   繁体   English

是否可以在不使用malloc的情况下进行内存泄漏?

[英]Is it possible to make memory leak without using malloc?

This question is as in title: Is it possible to produce a memory leak without using any kernel specific means like malloc, new, etc? 这个问题如标题所示:是否有可能在不使用任何内核特定方法(如malloc,new等)的情况下产生内存泄漏?

What if I will make a linked list inside a function with lot of elements in there, and after it I'll exit from this function without cleaning a list. 如果我在一个包含很多元素的函数内部创建一个链表怎么办,之后我会退出这个函数而不清理列表。 The list will be created without using any malloc calls, ie 该列表将在不使用任何malloc调用的情况下创建,即

struct list_head {
     struct list_head *next, *prev;
}

Can it be guaranteed that all resources will be freed after exiting from this function? 可以保证在退出此功能后将释放所有资源吗? So I can freely execute it a million times and nothing will be leaked? 所以我可以自由地执行它一百万次,什么都不会被泄露?

Subject: If you not using any particular malloc or new calls you won't get a heap memory leak. 主题:如果您不使用任何特定的malloc或新调用,则不会出现堆内存泄漏。 Never. 决不。 Is that right? 那正确吗?

A leak is always connected to a resource . 泄漏始终连接到资源 A resource is by definition something that you acquire manually, and that you must release manually. 根据定义,资源是您手动获取的,并且您必须手动释放。 Memory is a prime example, but there are other resources, too (file handles, mutex locks, network connections, etc.). 内存是一个很好的例子,但也有其他资源(文件句柄,互斥锁,网络连接等)。

A leak occurs when you acquire a resource, but subsequently lose the handle to the resource so that nobody can release it. 获取资源时会发生泄漏,但随后会丢失资源的句柄,以便没有人可以释放它。 A lesser version of a leak is a "still-reachable" kind of situation where you don't release the resource, but you still have the handle and could release it. 较小版本的泄漏是一种“仍然可达”的情况,在这种情况下,您不释放资源,但您仍然拥有句柄并可以释放它。 That's mostly down to laziness, but a leak by contrast is always a programming error. 这主要是懒惰,但相反的泄漏总是一个编程错误。

Since your code never acquires any resources, it also cannot have any leaks. 由于您的代码从不获取任何资源,因此它也不会有任何泄漏。

The variables you applied without malloc or new is located at stack space in the memory. 您在没有malloc或new的情况下应用的变量位于内存中的堆栈空间。 So when the function returned, the variable is taken back. 因此,当函数返回时,变量将被取回。

On the other hand, the memory you applied with malloc or new is located at heap space. 另一方面,使用malloc或new应用的内存位于堆空间。 The system doesn't care whether you release the space or not. 系统不关心您是否释放空间。 In this situation, if you don't use free or delete, memory leak will happen. 在这种情况下,如果不使用free或delete,则会发生内存泄漏。

Subject: If you not using any particular malloc or new calls you won't get a heap memory leak. 主题:如果您不使用任何特定的malloc或新调用,则不会出现堆内存泄漏。 Never. 决不。 Is that right? 那正确吗?

That assumption is not entirely correct. 这种假设并不完全正确。 The problem is that the operating system itself (or other third party components you have to rely on) can have memory leaks as well. 问题是操作系统本身(或您必须依赖的其他第三方组件)也可能存在内存泄漏。 In that case you might not actively call malloc, but call other (operating system) functions which could leak. 在这种情况下,您可能不会主动调用malloc,而是调用可能泄漏的其他(操作系统)函数。

So your assumption depends on how strongly you consider such a thing. 所以你的假设取决于你对这种事情有多强烈的考虑。 You can argue that the OS/third party implementation is outside your domain, then this assumption would be correct. 你可以说OS /第三方实现在你的域之外,那么这个假设是正确的。 If you have a well defined system and your requirements are such that you have to garuantee a certain uptime, something like this may have to be considered as well. 如果你有一个定义明确的系统,并且你的要求是你必须加快一定的正常运行时间,那么也可能需要考虑这样的事情。

So the answer to this question ... 所以这个问题的答案......

Is it possible to make memory leak without using malloc? 是否可以在不使用malloc的情况下进行内存泄漏?

... is: ......是:

Yes, it is possible. 对的,这是可能的。

malloc() allocates memory from the heap, while space for string and struct literals ( string1 , string2 and those list_head 's) will be reserved at compile time at the stack. malloc()从堆中分配内存,而字符串和结构文字( string1string2和那些list_head )的空间将在堆栈的编译时保留。

Actually any memory allocated for a program (heap or stack) will be reclaimed by the kernel when the process exits (at *nix system at least). 实际上,当进程退出时(至少在* nix系统中),内核将回收为程序(堆或堆栈)分配的任何内存。

I would define memory leak as allocating memory on heap and without freeing it when your program exits. 我会将内存泄漏定义为在堆上分配内存,并在程序退出时不释放它。 This definition actually answers your question. 这个定义实际上回答了你的问题

There are standard functions (like strdup ) that will allocate memory on heap, beware of them. 有一些标准函数(比如strdup )会在堆上分配内存,要小心它们。

Another example of a resource that you can allocate and forget to free: 您可以分配并忘记释放的资源的另一个示例:

If you're using OpenGL, and you call glGenBuffers() a million times without the corresponding glDeleteBuffers calls, it's extremely likely that you will run out of VRAM and your graphics driver will start leaking to system memory. 如果您正在使用OpenGL,并且在没有相应的glDeleteBuffers调用的情况下调用glGenBuffers()一百万次,那么很可能您将耗尽VRAM并且您的图形驱动程序将开始泄漏到系统内存。

I just had this happen. 我发生了这件事。 Fortunately, Visual Studio's memory profiler made it pretty easy to find. 幸运的是,Visual Studio的内存分析器使其很容易找到。 It showed up as a large number of allocations made by the external process nvoglv32.dll , which is my NVIDIA OpenGL driver. 它显示为外部进程nvoglv32.dll进行的大量分配,这是我的NVIDIA OpenGL驱动程序。

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

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