OpenMP内存泄漏的代码非常简单 - 如何查找未释放的块?

时间:2014-03-12 17:44:21

标签: c gcc memory-leaks openmp

我正在使用gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1编译此代码:gcc -fopenmp Untitled.c -o Untitled

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>

void dummy_function()
{
    int *m = malloc(sizeof(int) * 100);

    free(m);
    m = NULL;
}

int main (int argc, char *argv[])
{

    int nthreads, tid;

    /* Fork a team of threads giving them their own copies of variables */
    omp_set_num_threads(100);
    #pragma omp parallel
    {

        dummy_function();

        /* Obtain thread number */
        tid = omp_get_thread_num();
        printf("Hello World from thread = %d\n", tid);

        /* Only master thread does this */
        if (tid == 0)
        {
            nthreads = omp_get_num_threads();
            printf("\n\nNumber of threads = %d\n\n", nthreads);
        }

    } /* All threads join master thread and disband */

}

当我在valgrind下运行此代码时,我收到了以下消息:

==3026== Memcheck, a memory error detector
==3026== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3026== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==3026== Command: ./Untitled
==3026== Parent PID: 2817
==3026== 
--3026-- 
--3026-- Valgrind options:
--3026--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--3026--    --tool=memcheck
--3026--    --track-origins=yes
--3026--    --leak-check=yes
--3026--    --show-reachable=yes
--3026--    -v
--3026--    --track-fds=yes
--3026--    --log-file=errory.txt
--3026-- Contents of /proc/version:
--3026--   Linux version 3.11.0-18-generic (buildd@toyol) (gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu8) ) #32-Ubuntu SMP Tue Feb 18 21:11:14 UTC 2014
--3026-- Arch and hwcaps: AMD64, amd64-sse3-cx16-avx
--3026-- Page sizes: currently 4096, max supported 4096
--3026-- Valgrind library directory: /usr/lib/valgrind
--3026-- Reading syms from /home/mirx/Downloads/Untitled
--3026-- Reading syms from /lib/x86_64-linux-gnu/ld-2.17.so
--3026--   Considering /lib/x86_64-linux-gnu/ld-2.17.so ..
--3026--   .. CRC mismatch (computed 51e60a7a wanted 521e3c74)
--3026--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.17.so ..
--3026--   .. CRC is valid
--3026--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.17.so ..
--3026--   .. CRC is valid
--3026-- Reading syms from /usr/lib/valgrind/memcheck-amd64-linux
--3026--   Considering /usr/lib/valgrind/memcheck-amd64-linux ..
--3026--   .. CRC mismatch (computed c436d3d9 wanted 99aa3883)
--3026--    object doesn't have a symbol table
--3026--    object doesn't have a dynamic symbol table
--3026-- Scheduler: using generic scheduler lock implementation.
--3026-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--3026-- Reading suppressions file: /usr/lib/valgrind/default.supp
==3026== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-3026-by-mirx-on-???
==3026== embedded gdbserver: writing to   /tmp/vgdb-pipe-to-vgdb-from-3026-by-mirx-on-???
==3026== embedded gdbserver: shared mem   /tmp/vgdb-pipe-shared-mem-vgdb-3026-by-mirx-on-???
==3026== 
==3026== TO CONTROL THIS PROCESS USING vgdb (which you probably
==3026== don't want to do, unless you know exactly what you're doing,
==3026== or are doing some strange experiment):
==3026==   /usr/lib/valgrind/../../bin/vgdb --pid=3026 ...command...
==3026== 
==3026== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==3026==   /path/to/gdb ./Untitled
==3026== and then give GDB the following command
==3026==   target remote | /usr/lib/valgrind/../../bin/vgdb --pid=3026
==3026== --pid is optional if only one valgrind process is running
==3026== 
--3026-- REDIR: 0x4018f40 (strlen) redirected to 0x3806e491 (???)
--3026-- Reading syms from /usr/lib/valgrind/vgpreload_core-amd64-linux.so
--3026--   Considering /usr/lib/valgrind/vgpreload_core-amd64-linux.so ..
--3026--   .. CRC mismatch (computed 826470ab wanted a8b7e9f3)
--3026--    object doesn't have a symbol table
--3026-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
--3026--   Considering /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ..
--3026--   .. CRC mismatch (computed d4fd7975 wanted b90a895a)
--3026--    object doesn't have a symbol table
--3026-- REDIR: 0x4018db0 (index) redirected to 0x4c2d480 (index)
--3026-- REDIR: 0x4018e30 (strcmp) redirected to 0x4c2e560 (strcmp)
--3026-- Reading syms from /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0
--3026--   Considering /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0 ..
--3026--   .. CRC mismatch (computed d39c8599 wanted 576ffbf9)
--3026--    object doesn't have a symbol table
--3026-- Reading syms from /lib/x86_64-linux-gnu/libpthread-2.17.so
--3026--   Considering /lib/x86_64-linux-gnu/libpthread-2.17.so ..
--3026--   .. CRC mismatch (computed 8b3f6674 wanted 0d32a1a8)
--3026--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/libpthread-2.17.so ..
--3026--   .. CRC is valid
--3026--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/libpthread-2.17.so ..
--3026--   .. CRC is valid
--3026-- Reading syms from /lib/x86_64-linux-gnu/libc-2.17.so
--3026--   Considering /lib/x86_64-linux-gnu/libc-2.17.so ..
--3026--   .. CRC mismatch (computed 2c8ee98b wanted da4a7364)
--3026--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.17.so ..
--3026--   .. CRC is valid
--3026--   Considering /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.17.so ..
--3026--   .. CRC is valid
--3026-- REDIR: 0x52ed980 (strcasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--3026-- REDIR: 0x52e9d10 (strnlen) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--3026-- REDIR: 0x52efc50 (strncasecmp) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--3026-- REDIR: 0x52ec790 (memset) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--3026-- REDIR: 0x52ec740 (memcpy@GLIBC_2.2.5) redirected to 0x4a25720 (_vgnU_ifunc_wrapper)
--3026-- REDIR: 0x52eb700 (__GI_strrchr) redirected to 0x4c2d2a0 (__GI_strrchr)
--3026-- REDIR: 0x52e9c30 (__GI_strlen) redirected to 0x4c2d830 (__GI_strlen)
--3026-- REDIR: 0x52ec7d0 (__GI_memset) redirected to 0x4c2fb00 (memset)
--3026-- REDIR: 0x52e3470 (malloc) redirected to 0x4c2a270 (malloc)
--3026-- REDIR: 0x52e39f0 (realloc) redirected to 0x4c2c630 (realloc)
--3026-- REDIR: 0x52e3fb0 (calloc) redirected to 0x4c2c410 (calloc)
--3026-- REDIR: 0x52e3900 (free) redirected to 0x4c2b5a0 (free)
--3026-- REDIR: 0x52f3b20 (strchrnul) redirected to 0x4c300f0 (strchrnul)
==3026== 
==3026== FILE DESCRIPTORS: 4 open at exit.
==3026== Open file descriptor 3: /home/mirx/Downloads/errory.txt
==3026==    <inherited from parent>
==3026== 
==3026== Open file descriptor 2: /dev/pts/7
==3026==    <inherited from parent>
==3026== 
==3026== Open file descriptor 1: /dev/pts/7
==3026==    <inherited from parent>
==3026== 
==3026== Open file descriptor 0: /dev/pts/7
==3026==    <inherited from parent>
==3026== 
==3026== 
==3026== HEAP SUMMARY:
==3026==     in use at exit: 42,160 bytes in 103 blocks
==3026==   total heap usage: 203 allocs, 100 frees, 82,160 bytes allocated
==3026== 
==3026== Searching for pointers to 103 not-freed blocks
==3026== Checked 830,384,816 bytes
==3026== 
==3026== 104 bytes in 1 blocks are still reachable in loss record 1 of 5
==3026==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3026==    by 0x4E387A8: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4E3CDDA: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4E3897C: omp_set_num_threads (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4008A1: main (in /home/mirx/Downloads/Untitled)
==3026== 
==3026== 192 bytes in 1 blocks are still reachable in loss record 2 of 5
==3026==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3026==    by 0x4E387A8: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4E3CBDE: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4008C3: main (in /home/mirx/Downloads/Untitled)
==3026== 
==3026== 808 bytes in 1 blocks are still reachable in loss record 3 of 5
==3026==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3026==    by 0x4C2C74F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3026==    by 0x4E387F8: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4E3CA4A: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4008C3: main (in /home/mirx/Downloads/Untitled)
==3026== 
==3026== 12,544 bytes in 1 blocks are still reachable in loss record 4 of 5
==3026==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3026==    by 0x4E387A8: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4E3C525: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4E3B48B: GOMP_parallel_start (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4008C3: main (in /home/mirx/Downloads/Untitled)
==3026== 
==3026== 28,512 bytes in 99 blocks are possibly lost in loss record 5 of 5
==3026==    at 0x4C2C494: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3026==    by 0x4012578: _dl_allocate_tls (dl-tls.c:296)
==3026==    by 0x504B795: pthread_create@@GLIBC_2.2.5 (allocatestack.c:579)
==3026==    by 0x4E3C955: ??? (in /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0)
==3026==    by 0x4008C3: main (in /home/mirx/Downloads/Untitled)
==3026== 
==3026== LEAK SUMMARY:
==3026==    definitely lost: 0 bytes in 0 blocks
==3026==    indirectly lost: 0 bytes in 0 blocks
==3026==      possibly lost: 28,512 bytes in 99 blocks
==3026==    still reachable: 13,648 bytes in 4 blocks
==3026==         suppressed: 0 bytes in 0 blocks
==3026== 
==3026== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--3026-- 
--3026-- used_suppression:      2 dl-hack3-cond-1
==3026== 
==3026== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

如何清理那些内存泄漏?他们出来了,这是我的“错误”还是它的“错误”?如何摆脱它们?

0 个答案:

没有答案