如果仅使用以下两行创建程序:
PGconn *conn = PQconnectdb("user=dbtest password=dbtest dbname=testdb host=localhost port=5432");
PQfinish(conn);
像这样通过valgrind运行它:
valgrind --leak-check=full --show-leak-kinds=all ./program
您看到的是
==25388== Memcheck, a memory error detector
==25388== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25388== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==25388== Command: ./program
==25388==
==25388==
==25388== HEAP SUMMARY:
==25388== in use at exit: 109 bytes in 2 blocks
==25388== total heap usage: 6,948 allocs, 6,946 frees, 666,147 bytes allocated
==25388==
==25388== 13 bytes in 1 blocks are still reachable in loss record 1 of 2
==25388== at 0x483874F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==25388== by 0x4CB0635: CRYPTO_strdup (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==25388== by 0x4BD8AFB: BIO_meth_new (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==25388== by 0x488BE8B: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x488D19C: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x487507F: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x4875E3E: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x4878CD7: PQconnectdb (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x109158: main (in /workspace/test/src/program)
==25388==
==25388== 96 bytes in 1 blocks are still reachable in loss record 2 of 2
==25388== at 0x483874F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==25388== by 0x4CA70E8: CRYPTO_zalloc (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==25388== by 0x4BD8ADF: BIO_meth_new (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
==25388== by 0x488BE8B: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x488D19C: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x487507F: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x4875E3E: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x4878CD7: PQconnectdb (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
==25388== by 0x109158: main (in /workspace/test/src/program)
==25388==
==25388== LEAK SUMMARY:
==25388== definitely lost: 0 bytes in 0 blocks
==25388== indirectly lost: 0 bytes in 0 blocks
==25388== possibly lost: 0 bytes in 0 blocks
==25388== still reachable: 109 bytes in 2 blocks
==25388== suppressed: 0 bytes in 0 blocks
==25388==
==25388== For counts of detected and suppressed errors, rerun with: -v
==25388== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我注意到打开和关闭更多连接似乎并不会影响丢失或仍然可以访问的字节数。
我对C不太熟悉,所以我不得不问:
为什么字节仍然可以到达?当我关闭与数据库的连接时,我希望一切都将被释放。
答案 0 :(得分:1)
这些分配是在libcrypto
初始化期间执行的,postgresql客户端使用该初始化以便通过SSL / TLS与服务器通信。初始化仅执行一次;打开多少个连接都没有关系。 (但是,如果连接使用不同的密码,则可能会有其他初始化。)
Libcrypto确实提供了可用于释放初始化期间分配的存储的接口。这些内容记录在OpenSSL documentation中。因此,您可以尝试将它们添加到您的代码中。但是,老实说,除了使valgrind报告更清晰外,这实际上并不重要。