在getpwnam中是否有内存泄漏的修复或解决方法? [英] Is there a fix or a workaround for the memory leak in getpwnam?

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

问题描述

在getpwnam中是否有解决方法或解决方法?

Is there a fix or a workaround for the memory leak in getpwnam?

推荐答案

getpwnam ) 不会遭受内存泄漏。后续调用确实会覆盖其静态内部缓冲区。

getpwnam() does not suffer of memory leak. Subsequent calls, indeed, will overwrite its static internal buffer.

此类函数改为不可重入,因此非线程安全。 Paul建议使用 getpwnam_r()这是可重入版本,可以安全地在多线程上下文中使用。

Such kind of functions are instead non-reentrant and therefore non-thread safe. Paul suggested the use of getpwnam_r() which is the reentrant version, that is safe to be used in a multithread context.

也就是说,内存泄漏是由那些通过 malloc()分配内存的系统调用引起的,并使应用程序对免费()一旦使用了返回的数据。

That said, memory leaks are caused by those system calls that allocate memory by means of malloc() and leave the application the responsability to free() the memory once the returned data has used.

在这些情况下,为了不忘记释放分配的内存,建议使用RAII成语 - 参见异常安全。 std :: tr1 :: shared_ptr<> 也是一种可行的方法:对于shared_ptr,必须提供自定义删除程序到 code>当shared_ptr超出范围时的原始指针。

In these cases the RAII idiom is advisable in order to not forget to free the allocated memory -- see exception safety. std::tr1::shared_ptr<> is also a viable way: For the shared_ptr a custom deleter must be provided to free() the raw pointer when the shared_ptr goes out of the scope.

在这个透视下,一些危险的函数是 scandir() asprintf code>, vasprintf()等。

Under this perspective some dangerous functions are scandir(), asprintf(), vasprintf() etc.

这篇关于在getpwnam中是否有内存泄漏的修复或解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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