什么时候可以发生内存泄漏? [英] When can a memory leak occur?

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

问题描述

我不知道在这里想什么...

I don't know what to think here...

我们有一个作为服务运行的组件。它在我的本地机器上运行得很好,但是在其他机器上(在两个机器RAM上都等于2GB),它在第二天连续产生bad_alloc异常。事情是,进程的内存使用保持不变,大约50Mb级别。另一个奇怪的事情是,通过跟踪消息,我们已经本地化的异常抛出一个字符串流对象,但不插入不超过1-2 Kb的数据到流。我们正在使用STL-Port,如果这很重要。

We have a component that runs as a service. It runs perfectly well on my local machine, but on some other machine (on both machine RAM's are equal to 2GB) it starts to generate bad_alloc exceptions on the second and consecutive days. The thing is that the memory usage of the process stays the same at aproximately 50Mb level. The other weird thing is that by means of tracing messages we have localized the exception to be thrown from a stringstream object which does but insert no more than 1-2 Kb data into the stream. We're using STL-Port if that matters.

现在,当你得到一个bad_alloc异常,你认为这是一个内存泄漏。但是 全部 我们的手动分配被包装成一个智能指针。另外,我不能理解一个stringstream对象如何缺少内存,当整个过程只使用〜50Mb(内存使用保持近似常数(确定不会上升)一天)。

Now, when you get a bad_alloc exception, you think it's a memory leak. But all our manual allocations are wrapped into a smart pointer. Also, I can't understand how a stringstream object lacks memory when the whole process uses only ~50Mb (the memory usage stays approximtely constant(and sure doesn't rise) from day to day).

我不能为你提供代码,因为项目真的很大,抛出异常的部分没有什么别的,只是创建一个字符串流<<一些数据然后记录它。

I can't provide you with code, because the project is really big, and the part which throws the exception really does nothing else but create a stringstream and << some data and then log it.

所以,我的问题是...当进程在2GB内只使用50Mb内存时,如何发生内存泄漏/ bad_alloc?你还有什么其他野生猜测,可能是什么错误?

So, my question is... How can a memory leak/bad_alloc occur when the process uses only 50Mb memory out of 2GB ? What other wild guesses do you have as to what could possibly be wrong?

提前感谢,我知道这个问题是模糊的,我只是一种绝望我尽力解释这个问题。

Thanks in advance, I know the question is vague etc., I'm just sort of desperate and I tried my best to explain the problem.

推荐答案

bad_alloc 并不一定意味着没有足够的内存。分配函数也可能失败,因为堆已损坏。

bad_alloc doesn't necessarily mean there is not enough memory. The allocation functions might also fail because the heap is corrupted. You might have some buffer overrun or code writing into deleted memory, etc.

您也可以使用 Valgrind 或其中一个 Windows更换找到泄漏/溢出。

You could also use Valgrind or one of its Windows replacements to find the leak/overrun.

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

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