内存泄漏与Eigen + std :: vector + OpenMP

时间:2015-11-06 09:40:04

标签: c++ vector memory-leaks openmp

我对Eigen有一个问题,这让我发疯。每当我尝试重新分配一组存储在std :: vector中的Eigen :: MatrixXd时,我就会一直从valgrind中获取内存泄漏。

我已经重写了我的代码至少5次,我尝试过使用std :: maps(这是我初始解决方案的一部分),我已经从矢量向量转移到普通向量......没有任何效果。一旦我添加了我的openMP指令,我就会泄漏。

这是一个最小的工作示例:

#include <iostream>
#include <Eigen/Dense>
#include <vector>
#include <omp.h>

typedef Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > EMat;

EMat function() {
        EMat a(19,29);
        return a;
}

int main() {
        std::vector < EMat > v(10);

#pragma omp parallel for schedule(dynamic)
        for (unsigned int i = 0; i < 10; ++i) {
                v[i] = function();
        }
}

如果我尝试用valgrind运行它,我得到:

r@darkstar ~/tmp $ valgrind --leak-check=full --show-leak-kinds=all ./test_leak==9640== Memcheck, a memory error detector
==9640== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==9640== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==9640== Command: ./test_leak
==9640== 
==9640== 
==9640== HEAP SUMMARY:
==9640==     in use at exit: 4,632 bytes in 10 blocks
==9640==   total heap usage: 31 allocs, 21 frees, 48,952 bytes allocated
==9640== 
==9640== 72 bytes in 1 blocks are still reachable in loss record 1 of 4
==9640==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9640==    by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9640==    by 0x513F7F8: gomp_realloc (alloc.c:54)
==9640==    by 0x51439FA: gomp_team_start (team.c:376)
==9640==    by 0x51412E9: GOMP_parallel_loop_dynamic_start (loop.c:466)
==9640==    by 0x400EBD: main (test_leak.cpp:19)
==9640== 
==9640== 192 bytes in 1 blocks are still reachable in loss record 2 of 4
==9640==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9640==    by 0x513F7A8: gomp_malloc (alloc.c:36)
==9640==    by 0x5143B8E: gomp_team_start (team.c:190)
==9640==    by 0x51412E9: GOMP_parallel_loop_dynamic_start (loop.c:466)
==9640==    by 0x400EBD: main (test_leak.cpp:19)
==9640== 
==9640== 2,128 bytes in 7 blocks are possibly lost in loss record 3 of 4
==9640==    at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9640==    by 0x4012E54: _dl_allocate_tls (dl-tls.c:296)
==9640==    by 0x5C33DA0: pthread_create@@GLIBC_2.2.5 (allocatestack.c:589)
==9640==    by 0x5143905: gomp_team_start (team.c:439)
==9640==    by 0x51412E9: GOMP_parallel_loop_dynamic_start (loop.c:466)
==9640==    by 0x400EBD: main (test_leak.cpp:19)
==9640== 
==9640== 2,240 bytes in 1 blocks are still reachable in loss record 4 of 4
==9640==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==9640==    by 0x513F7A8: gomp_malloc (alloc.c:36)
==9640==    by 0x51434D5: gomp_new_team (team.c:144)
==9640==    by 0x51405FC: gomp_parallel_loop_start (loop.c:447)
==9640==    by 0x51412E9: GOMP_parallel_loop_dynamic_start (loop.c:466)
==9640==    by 0x400EBD: main (test_leak.cpp:19)
==9640== 
==9640== LEAK SUMMARY:
==9640==    definitely lost: 0 bytes in 0 blocks
==9640==    indirectly lost: 0 bytes in 0 blocks
==9640==      possibly lost: 2,128 bytes in 7 blocks
==9640==    still reachable: 2,504 bytes in 3 blocks
==9640==         suppressed: 0 bytes in 0 blocks
==9640== 
==9640== For counts of detected and suppressed errors, rerun with: -v
==9640== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

我花了几个小时在网上搜索并试验但没有运气。 有人可以帮帮我吗?

提前致谢!

[R

1 个答案:

答案 0 :(得分:3)

这不是因为Eigen也不是plugin.xml,而是因为OpenMP&#34;泄漏&#34;或者说valgrind报告不可信任&#34;。

如上所述,不要在此花费太多时间,你不对这些泄漏负责。

参见相关链接:

  

据我所知,libgomp并没有在执行结束时杀死它产生的线程并使内核清理干净。

  

在libgomp的情况下,大多数分配仍然可以在退出时间到达   落入他们真正无法释放的范畴。