Valgrind的合法"可能失去"例如字节 [英] Valgrind legitimate "possibly lost" bytes example

查看:96
本文介绍了Valgrind的合法"可能失去"例如字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的valgrind分类内存泄漏到:

I saw that valgrind classifies memory leaks into:


  • 绝对失去

  • 间接失去

  • 可能失去

  • 仍可达

  • 燮pressed

我只是固定泄漏这里的可能失去是主要问题。

I just fixed a leak where the "possibly lost" was the main problem.

借助文档说可能失去意味着你的程序正在泄漏内存,除非你做不寻常的事情有可能导致它们指向到一个分配块的中间指针;请参阅用户手册中一些可能的原因

The documentation says: "possibly lost means your program is leaking memory, unless you're doing unusual things with pointers that could cause them to point into the middle of an allocated block; see the user manual for some possible causes"

我可以请知道的一个例子做不寻常的事情有可能导致它们指向到一个分配块的中间指针的?

May I please know an example of "doing unusual things with pointers that could cause them to point into the middle of an allocated block" ?

我的意思是,其中可能失去可尽管它是由Valgrind的报告忽略了一个例子。在该指针的使用使得Valgrind的一个例子抱怨,但在同一时间以这种方式使用该指针是某种合法

I mean an example where "possibly lost" can be ignored although it is reported by valgrind. An example in which the use of pointers makes valgrind complain but at the same time the use of the pointers in that way is somehow legitimate

感谢您

推荐答案

的文档什么的一些例子是有自己的分配器和它返回的内存是不能直接通过底层操作系统分配器(的malloc返回的指针不同的库/ SBRK),但是偏移后的指针。考虑,例如,所获得的一些额外的存储器和存储的元信息的分配器(也许为垃圾收集型信息......)。分配和释放的过程将类似于:

Some examples of what the documentation are different libraries that have their own allocators and for which the memory returned is not directly the pointer returned by the underlying OS allocator (malloc/sbrk), but a pointer after an offset. Consider for example, an allocator that obtained some extra memory and stored meta information (maybe type information for a garbage collector...). The process of allocation and deallocation would be similar to:

void* allocate( size_t size ) {
   metainfo_t *m = (metainfo_t*) malloc( size + sizeof(metainfo) );
   m->data = some_value;
   return (void*)(m+1);          // [1]
}
void deallocate( void* p ) {
   metainfo_t *m = ((metainfo_t*)p) - 1;
   // use data
}
void * memory = allocate(10);

在Valgrind是跟踪的记忆,它会记住,是由的malloc 返回原来的指针,该指针不存储在程序中。但是,这并不意味着该存储器已经被泄露,则仅意味着该指针不直接在程序可用。特别是内存仍持有返回的指针,而取消分配可调用来释放它,但的valgrind 没有看到原始返回的位置指针(字符*)内存 - 的sizeof(metadata_t)在程序的任何地方,并警告

When valgrind is tracking the memory, it remembers the original pointer that was returned by malloc, and that pointer is not stored anywhere in the program. But that does not mean that the memory has been leaked, it only means that the pointer is not directly available in the program. In particular memory still holds the returned pointer, and deallocate can be called to release it, but valgrind does not see the original returned pointer at location (char*)memory - sizeof(metadata_t) anywhere in the program and warns.

这篇关于Valgrind的合法"可能失去"例如字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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