2

-lcairoとにリンクするプログラムのメモリの問題を追跡してきました-lX11。最後に、すべての行をコメントアウトしてmain()、問題がないことを確認することにしvalgrindました。驚いたことに、そうではありません:

==7570== Memcheck, a memory error detector
==7570== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7570== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==7570== Command: ./Test
==7570== 
==7570== 
==7570== HEAP SUMMARY:
==7570==     in use at exit: 10,360 bytes in 5 blocks
==7570==   total heap usage: 5 allocs, 0 frees, 10,360 bytes allocated
==7570== 
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 1 of 5
==7570==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570==    by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FCBACE: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FCD585: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570==    by 0x4010222: _dl_init (dl-init.c:36)
==7570==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570== 
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 2 of 5
==7570==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570==    by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FCA61F: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FCD5A0: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570==    by 0x4010222: _dl_init (dl-init.c:36)
==7570==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570== 
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 3 of 5
==7570==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570==    by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FE5A8F: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FBC1A5: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FCD5AB: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570==    by 0x4010222: _dl_init (dl-init.c:36)
==7570==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570== 
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 4 of 5
==7570==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570==    by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x60053CF: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FCD5AB: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570==    by 0x4010222: _dl_init (dl-init.c:36)
==7570==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570== 
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 5 of 5
==7570==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570==    by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5FCFCBF: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570==    by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570==    by 0x4010222: _dl_init (dl-init.c:36)
==7570==    by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570== 
==7570== LEAK SUMMARY:
==7570==    definitely lost: 0 bytes in 0 blocks
==7570==    indirectly lost: 0 bytes in 0 blocks
==7570==      possibly lost: 0 bytes in 0 blocks
==7570==    still reachable: 10,360 bytes in 5 blocks
==7570==         suppressed: 0 bytes in 0 blocks
==7570== 
==7570== For counts of detected and suppressed errors, rerun with: -v
==7570== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

空の割り当てメモリを持つプログラムは、main()到達可能なブロックで終わる可能性がありますか? この奇妙な問題を取り除く方法はありますか?

4

1 に答える 1

2

はい。可能です。

1 つの方法は、リンクしているライブラリにいくつかの静的オブジェクトがある可能性があることです。これはすでに@MMによって指摘されています

もう 1 つの方法は、静的オブジェクトを使用せずに共有ライブラリにリンクし、そのようなライブラリ内の特定の関数が を使用する場合__attribute__((constructor))です。この属性により、これらの関数は main() (gcc でのみ機能) の前に実行されるため、メモリ割り当てが表示されます。これらの関数は、プログラムで使用される場合に、関数に関する特定の属性を宣言するために、主にライブラリによって使用されます。これらの属性の詳細については、次を参照してください。

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

于 2015-11-22T21:27:15.670 に答える