C++ valgrind 条件跳转或移动取决于未初始化的值,这是否表示内存泄漏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14952637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:50:34  来源:igfitidea点击:

valgrind Conditional jump or move depends on uninitialised value(s) , does this indicate memory leak?

c++cvalgrind

提问by user862833

I am facing a memory leak problem in the code, while its running, the heap goes on increasing to maximum and i need to restart the service, I ran top command and see that the heap is increasing whenever im invoking a scenario in the service.

我在代码中遇到内存泄漏问题,在运行时,堆继续增加到最大值,我需要重新启动服务,我运行 top 命令并看到每当我调用服务中的场景时堆都在增加。

I ran the service with valgrind ,

我用 valgrind 运行服务,

valgrind  --log-file=log-feb19.txt --leak-check=full --show-reachable=yes --track-origins=yes myservice

I donot see any definitely lost or possibly lost blocks while iam running the scenarios but i see a lot of Conditional jump or move depends on uninitialised value(s) errors.

我在运行场景时没有看到任何肯定丢失或可能丢失的块,但我看到很多条件跳转或移动取决于未初始化的值错误。

Do these count for a memory leak?

这些算不算内存泄漏?

Example of what i am getting:

我得到的例子:

==27278== Conditional jump or move depends on uninitialised value(s)

==27278==    at 0xC90D91E: xcsFreeMemFn (in /apps/opt/mqm/lib64/libmqmcs_r.so)

........

…………

==27278==  Uninitialised value was created by a heap allocation

==27278==    at 0x4A078B8: malloc (vg_replace_malloc.c:270)

==27278==    by 0xC90E32F: xcsGetMemFn (in /apps/opt/mqm/lib64/libmqmcs_r.so)

Can someone help.

有人可以帮忙吗。

回答by sehe

No, it means that you are accessing memory that hasn't been initialized:

不,这意味着您正在访问尚未初始化的内存:

int main()
{
     int unitialized;
     std::cout << unitialized << std::endl;
}

would trigger this error.

会触发这个错误。

Slightly more common would be:

稍微更常见的是:

struct X 
{
     X() : i(42) { }
  private:
     int i;
     int* forgotten; // oops, not initialized
};

Lastly, this frequently happens with malloc based code, when you don't use memsetto clear the whole buffer. So,

最后,当您不使用memset清除整个缓冲区时,这经常发生在基于 malloc 的代码中。所以,

  1. malloc a buffer size m
  2. read (e.g. from a socket) n bytes
  3. write m bytes to a file; (m-n) bytes wouldn't have been initialized
  1. malloc 一个缓冲区大小 m
  2. 读取(例如从套接字)n 个字节
  3. 将 m 个字节写入文件;(mn) 字节不会被初始化

回答by logoff

It is explained in Valgrind User Manual, in section 4.2.2. Use of uninitialised values:

Valgrind 用户手册中的4.2.2节对此进行了解释使用未初始化的值

An uninitialised-value use error is reported when your program uses a value which hasn't been initialised -- in other words, is undefined.

...

It is important to understand that your program can copy around junk (uninitialised) data as much as it likes. Memcheck observes this and keeps track of the data, but does not complain. A complaint is issued only when your program attempts to make use of uninitialised data in a way that might affect your program's externally-visible behaviour.

当您的程序使用尚未初始化的值时,会报告未初始化值使用错误——换句话说,未定义。

...

重要的是要了解您的程序可以随心所欲地复制垃圾(未初始化)数据。Memcheck 观察到这一点并跟踪数据,但没有抱怨。只有当您的程序试图以可能影响您程序的外部可见行为的方式使用未初始化的数据时,才会发出投诉。

回答by Ivaylo Strandjev

No this does not indicate memory leak directly. However having a conditional jump depending on a non-initialized variable may lead to practically anything. Using uninitialized variables in general invokes undefined behavior.

不,这并不直接表示内存泄漏。然而,根据未初始化的变量进行条件跳转实际上可能会导致任何结果。通常使用未初始化的变量会调用未定义的行为。