我运行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%的错误,都是因为这个!
有人知道怎么解决这个问题吗?

最佳答案

你是不是打开了很多数据库连接句柄却忘了释放它们?在使用libpq时,您应该始终释放PQconnectdb返回的指针所使用的内存,即使与服务器的连接没有成功。为此,用指向PQfinish的指针调用PGconn

关于c - 使用Valgrind检查内存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19484230/

10-09 09:01