什么是内存泄漏 [英] What is a memory leak

查看:47
本文介绍了什么是内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读关于异常安全的问题,并想知道什么时候需要打扰,

以及当程序崩溃而不占用一些资源时。如果我只是讨论在程序中分配的内存,那么我可以看到


(躲闪的头顶代码如下)


int main {

while(true){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

}


返回0;

}

.........


将填满内存,直到出现故障并崩溃,让我们说第1000次迭代


但是


int main(){


MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

exit(1); < br $>

}


是否遵循我只能运行此程序1000次?或者一旦退出,所有资源都是

?如果退出被一些

异常生成代码替换,有什么不同吗?


欢呼


shaun

I''m reading about exception safety and wondering when I need to bother,
and when a program can crash without hogging some resource. If I am only
talking about memory allocated during a program, then I can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}

return 0;
}
.........

will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times? Or are all
the resources released once it exits? If the exit is replaced by some
exception-generating code, is there any difference?

cheers

shaun

推荐答案

shaun roe写道:
shaun roe wrote:

我正在阅读有关异常安全的内容并且想知道什么时候我需要付出b $ b b b b b b b b b that that that when when when when when when when when when when when when when when when when when when when when when when
I''m reading about exception safety and wondering when I need to
bother, and when a program can crash without hogging some resource.



这是一个精心伪装的家庭作业吗?看起来像......

Is this a well-masqueraded homework? Looks like it...


如果我只谈论在程序中分配的内存,那么我可以看到

(狡猾的头顶代码如下)


int main {

while(true){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

}


返回0;

}

........


会填满​​内存,直到出现问题并且崩溃为止,让我们说第(0)次
第1000次迭代





int main(){


MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

退出(1);


}


是否遵循我的意思只能运行这个程序1000次?
If I am only talking about memory allocated during a program, then I
can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}

return 0;
}
........

will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times?



通常不会。

Usually not.


或者都是

一次发布的资源它会退出吗?如果退出被一些

异常生成代码替换,有什么区别吗?
Or are all
the resources released once it exits? If the exit is replaced by some
exception-generating code, is there any difference?



这在很大程度上取决于执行环境。一些免费程序

内存一旦程序完成并将其返回到可用内存,

一些不会。


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问

It highly depends on the execution environment. Some free program
memory once program finishes and return it to the available memory,
some don''t.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




shaun roe写道:

shaun roe wrote:

我正在读关于异常安全的问题,想知道什么时候我需要打扰,

以及当程序崩溃而不会占用一些资源时。如果我只是讨论在程序中分配的内存,那么我可以看到


(躲闪的头顶代码如下)


int main {

while(true){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

}
I''m reading about exception safety and wondering when I need to bother,
and when a program can crash without hogging some resource. If I am only
talking about memory allocated during a program, then I can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}



只是一点点狡猾的主要宣言:)

Just a little bit of dodgy declaration of main :)


>

返回0;

}

........
>
return 0;
}
........



默认行为new是在内存分配失败时抛出badalloc。因此,返回0将永远不会发生,并且您的

程序将以未处理的异常退出。当它这样做时,我认为对象的解构器被称为退出的一部分

处理程序(我很可能是错的)。但是,很可能你的操作系统会以b $ b释放所获得的资源。

The default behaviour of new is to throw a badalloc when memory
allocation fails. Therefore, return 0 will never happen, and your
program will exit with an unhandled exception. When it does that I
think the deconstructors for objects are called as part of the exit
handler (I could well be wrong). However, most likely your OS will
release resources acquired otherwise.


>

将填满内存直到某些事情发生故障并且崩溃,让我们说第1000次迭代



但是


int main(){


MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

exit(1);


}


是否遵循我只能运行此程序1000次?或者一旦退出,所有资源都是

?如果退出被一些

异常生成代码替换,有什么区别吗?
>
will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times? Or are all
the resources released once it exits? If the exit is replaced by some
exception-generating code, is there any difference?



一般来说,你想要处于一个位置你的程序发布的地方

资源,因为它不再需要它们。但是,从

main,或exit()或未处理的异常返回时,它们通常由操作系统释放。

操作系统 - 如果程序不这样做的话就这样了......


Jon。

Generally, you want to be in a position where your program releases
resources as it no longer requires them. However, on a return from
main, or exit() or unhandled exception, they are generally released by
the OS - if the program doesn''t do that ''on it''s way out...''

Jon.


shaun roe写道:
shaun roe wrote:

我正在读关于异常安全的问题,想知道我什么时候需要打扰,

以及程序何时崩溃而不会占用一些资源。如果我只是讨论在程序中分配的内存,那么我可以看到


(躲闪的头顶代码如下)


int main {

while(true){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

}


返回0;

}

........


会填满记忆,直到出现故障并且崩溃,让我们说第1000次迭代






int main(){


MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;

exit(1);


}


是否遵循我只能运行此程序1000次?
I''m reading about exception safety and wondering when I need to bother,
and when a program can crash without hogging some resource. If I am only
talking about memory allocated during a program, then I can see that

(dodgy top-of-head code follows)

int main{
while (true){
MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
}

return 0;
}
........

will fill up memory until something breaks and it crashes, lets say on
the 1000th iteration

but what about:

int main(){

MemoryHoggingObject * myObjectPtr = new MemoryHoggingObject;
exit(1);

}

Does it follow that I can only run this program 1000 times?



No.

No.


或者一旦退出就释放所有资源?
Or are all the resources released once it exits?



C ++标准没有说明获取或返回内存到

操作系统。它(明智地?)避免对计算环境提供的支持

做出这样的要求。但是,我不知道任何操作系统

在程序终止时不会回收资源。

The C++ standard says nothing about obtaining nor returning memory to the
OS. It refrains (wisely?) from making such requirements about the support
offered by the computation environment. However, I do not know of any OS
that will not reclaim resources upon termination of a program.


如果退出被替换为一些异常生成代码,是否有任何

的区别?
If the exit is replaced by some exception-generating code, is there any
difference?



不符合标准。即使你为你的程序调用所有指针

你的新() - ed,也无法保证将内存返回给操作系统。
返回给操作系统。

最佳


Kai-Uwe Bux

Not in terms of the standard. Even if you call delete() for all the pointers
you new()-ed in your program, there is no guarantee that memory will be
returned to the OS.
Best

Kai-Uwe Bux


这篇关于什么是内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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