Windows pthread中的内存泄漏. pthread_join不会释放内存 [英] Memory leakage in windows pthread. `pthread_join` does not deallocate memory

查看:309
本文介绍了Windows pthread中的内存泄漏. pthread_join不会释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单测试:

void testMemoryLeak_PthreadCreateJoin(void)
{
   auto taskFunction = [](void*args) -> void*
   {
      return nullptr;
   };
   pthread_t pth;
   int err = pthread_create(&pth, /*attr*/nullptr, taskFunction, /*args*/nullptr);
   pthread_join(pth, nullptr);
}

void main(void)
{
   _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
   testMemoryLeak_PthreadCreateJoin();
   testMemoryLeak_PthreadCreateJoin();
   testMemoryLeak_PthreadCreateJoin();
   testMemoryLeak_PthreadCreateJoin();
}

此处说:

线程是已分配的资源,您没有在退出之前释放它.您应该致电pthread_join;这也将消除您的黑客行为和不正确的睡眠循环的需要.

A thread is an allocated resource and you did not free it before exiting. You should call pthread_join; this would also eliminate the need for your hackish and incorrect sleep loop.

即使解决了这个问题,由于POSIX线程的某些实现(我猜您使用的是glibc/NPTL),缓存和重用线程资源而不是完全释放它们也可能会使valgrind仍然出现泄漏". .我不确定valgrind是否可以解决此问题.

It's possible that even once you fix this, valgrind will still see a "leak", since some implementations of POSIX threads (I'm guessing you're using glibc/NPTL) cache and reuse thread resources rather than freeing them fully. I'm not sure if valgrind works around this or not.

但是我已经使用了pthread_join.我使用VS2015及其堆分析器.问题可能出在我的特定实现中吗?我用的是宋东升的PAL

But I already use pthread_join. I use VS2015 and its heap analyzer. Could the problem be in pthread my particular implementation? I use PAL by Dongsheng Song

导致内存泄漏:

Detected memory leaks!
Dumping objects ->
{104} normal block at 0x007742C0, 24 bytes long.
 Data: <     X          > D8 00 00 00 E0 58 20 00 00 00 00 00 00 00 00 00 
{101} normal block at 0x00774398, 24 bytes long.
 Data: <     X          > D8 00 00 00 E0 58 20 00 00 00 00 00 00 00 00 00 
{98} normal block at 0x00774038, 24 bytes long.
 Data: <     X          > D8 00 00 00 E0 58 20 00 00 00 00 00 00 00 00 00 
{95} normal block at 0x00774860, 24 bytes long.
 Data: <     X          > D8 00 00 00 E0 58 20 00 00 00 00 00 00 00 00 00 
Object dump complete.

每个pthread

24个字节. pthread_join()必须释放内存,但不是.所以我想实现是错误的.请确认或混淆.

24 bytes for every pthread. pthread_join() had to free memory but it didn't. So i suppose implementation is buggy. Please confirm or confute this.

推荐答案

如果要跟踪分配点,请参见

If you want to track down the allocation point see _CrtSetAllocHook - you can set your own allocation hook and examine the stack for the blocks you know that will be leaked. However for this to bring any benefits you would need a debug version of the POSIX implementation to correctly see the stack. Then you can try and actually patch it so that the memory is freed.

这篇关于Windows pthread中的内存泄漏. pthread_join不会释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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