我运行valgrind来查看程序对内存问题的错误。 valgrind的输出是:
==31041== 17,736 bytes in 1 blocks are still reachable in loss record 423 of 423
==31041== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31041== by 0x5B0F21F: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
==31041== by 0x5874B12: ??? (in /lib/x86_64-linux-gnu/libssl.so.1.0.0)
==31041== by 0x5874C68: ??? (in /lib/x86_64-linux-gnu/libssl.so.1.0.0)
==31041== by 0x586DE5B: ??? (in /lib/x86_64-linux-gnu/libssl.so.1.0.0)
==31041== by 0x4E4FFEA: ??? (in /usr/lib/libpq.so.5.4)
==31041== by 0x4E40AB5: PQconnectPoll (in /usr/lib/libpq.so.5.4)
==31041== by 0x4E41F4D: ??? (in /usr/lib/libpq.so.5.4)
==31041== by 0x4E428FE: PQconnectdb (in /usr/lib/libpq.so.5.4)
==31041== by 0x401D18: open_connection (database.c:23)
==31041== by 0x402A78: init (kernel.c:28)
==31041== by 0x402E9C: main (main.c:22)
90%的错误都与此有关!
有人知道如何解决这个问题吗?
答案 0 :(得分:1)
您是否打开了许多数据库连接句柄并忘记释放它们?使用libpq时,即使与服务器的连接未成功,也应始终释放PQconnectdb
返回的指针使用的内存。为此,请使用指向PQfinish
的指针调用PGconn
。