如果没有新关键字,是否可能发生内存泄漏? [英] Is it possible to have memory leak without new keyword?

查看:63
本文介绍了如果没有新关键字,是否可能发生内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解内存泄漏的原因是因为在堆上创建了一个对象,并且未使用delete关键字对其进行清理.因此,如果在项目中始终在堆栈中初始化一个类,则该类不会发生内存泄漏.如果始终不使用new关键字初始化此类的对象,则该类是否可能发生内存泄漏?

例如,我有一个CStudent类,在我的代码中,我总是使用CStudent stu初始化此类.我有一些向量来存储CStudent的对象,但我将它们定义为vector< CStudent>.我从不使用new关键字来创建CStudent对象.

What I understand the reason for memory leak is because an object is created on the heap and is not cleaned by using the delete keyword. So if in a project a class is always initialized in the stack there will be no memory leaks for this class. Is it possible to have memory leaks for a class if the objects of this class are always initialized without the new keyword?

For example, I have a class CStudent and in my code I always use CStudent stu to initialize this class. I have some vectors to store objects of CStudent but I define them as vector<CStudent>. I never use new keyword to create a CStudent object. So is it possible to have memory leaks of CSudent objects?

推荐答案

您的向量可能正在使用堆内存为CStudent对象分配空间,因此您可能仍然可以找到一个内存泄漏的方法.

你为什么要问??根据对象的大小以及有多少个对象,堆栈可能不适用于它们.再加上基于堆栈的对象仅在其创建功能的整个过程中持续存在.
Your vectors are probably using heap memory to allocate space for your CStudent objects so you could probably still find a way to leak memory.

Why do you ask?? Depending on the size of the objects and how many there is, the stack may not be the appropriate place for them. Plus your stack based objects only last for the duration of the function they are created in.


是的,如果没有关键字new,您可能会发生内存泄漏.
如果正确地清理了向量,您所解释的示例似乎不会导致内存泄漏,但是可能会有不同的情况.我将它们分为两类:内存分配低于 new级别和高于 new级别.在new级别,我的意思是嵌入在该语言中的内存分配/重新分配.

以下:C ++ new/delete是在OS平台上实现的.与OS的语言绑定通常基于系统堆的分配和释放.为了优化和减少碎片,可以使用 sub-allocator :可以在系统堆上大块分配一块内存;并且此内存被子分配给较小的对象.您的代码还可以直接使用语言绑定到OS,并且无需使用new即可分配内存.如果未正确回收此内存,则可能发生内存泄漏.另外,某些OS API可以间接分配内存.它需要一些清理OS的调用.如果未正确清理,则此类调用可能会导致某些内存泄漏.这种由于不正确使用OS绑定导致的间接内存泄漏与资源泄漏问题密切相关.

以上:您可以使用第三方库,这些库可以在语言级别(new)或操作系统级别分配内存.如果您在使用此类库时犯了错误,或者库中有错误,则可能导致内存泄漏.当您的源代码不可用时,您可能无法检测到此类库中的new.

一个实际的注意事项:由于编程错误而导致的内存泄漏的一个非常典型的来源是在出现异常的情况下无法回收内存.正确使用try-finally结构有助于避免这种情况.

-SA
Yes, you can have a memory leak without the key word new.

The example you explain does not look like it can cause a memory leak if the vector is cleaned up correctly, but there can be different scenarios. I would classify them into two classes: memory allocation below the new level and above the new level. By new level I mean memory allocation/deallocation embedded in the language.

Below: C++ new/delete is implemented on top of OS platform. Language binding with OS is usually based on allocation and deallocation of system heap. For optimization and reduce of fragmentation a sub-allocator can be used: a block of memory can be allocated on the system heap in big chunks; and this memory is sub-allocated for smaller objects. Your code can also use language binding to the OS directly and allocate memory without using new. Is this memory is not properly reclaimed, memory leak may occur. Also, some OS API can allocate memory indirectly. It requires some clean-up OS calls. If not cleaned-up correctly, such calls can cause some memory leaks. Such indirect memory leak cause by incorrect use of OS binding is closely related to the problem of resource leak.

Above: you can use third-party libraries which can allocate memory at the language level (new) or at the level of OS. If you make bugs in using of such libraries or if the libraries have bugs, it can cause memory leak. You may not be able to detect the presence of new in such libraries when their source code is not available to you.

One practical note: very typical source of memory leaks as a result of programming bug is the failure to reclaim memory in case of exception. Proper use of try-finally construct helps to avoid such situations.

—SA


http://msdn.microsoft.com/en-us/library/x98tx3cf(v=VS.80).aspx[^]

check this...


这篇关于如果没有新关键字,是否可能发生内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆