Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this simple libpqxx code leaking memory?
    primarykey
    data
    text
    <p>Recently I wrote an application that accesses a Postgres DB via libpqxx, and it leaks memory badly. Even this simple test program, which is based on the example at <a href="http://pqxx.org/devprojects/libpqxx/doc/4.0/html/Reference/a00001.html" rel="nofollow">http://pqxx.org/devprojects/libpqxx/doc/4.0/html/Reference/a00001.html</a> leaks like no tomorrow.</p> <p>(Edit: I added calls to commit() and clear() in response to suggestions. Same leakiness.)</p> <pre><code>#include &lt;iostream&gt; #include &lt;pqxx/pqxx&gt; #include &lt;string&gt; #include &lt;stdio.h&gt; int main() { try { pqxx::connection c("user=postgres"); int i = 0; while(true) { pqxx::work w(c); pqxx::result r = w.exec("SELECT 1"); w.commit(); i++; if ( i % 1000 == 0 ) printf( "Cycle %d\n", i ); r.clear(); } //while } //try catch (const std::exception &amp;e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; return 1; } //catch } //main </code></pre> <p>After about 75,000 cycles of the loop, top shows 206Mb of virtual memory usage, and it keeps climbing. I ran a similar test program with 5000 cycles through valgrind and got this:</p> <pre><code>==1647== 13,732,155 (219,868 direct, 13,512,287 indirect) bytes in 4,997 blocks are definitely lost in loss record 12 of 12 ==1647== at 0x40060D5: operator new(unsigned int) (vg_replace_malloc.c:214) ==1647== by 0x404C0A9: pqxx::result::result(pg_result*, int, std::string const&amp;, int) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x40309EF: pqxx::connection_base::make_result(pg_result*, std::string const&amp;) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x4036D65: ??? (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x405EFD6: pqxx::transaction_base::DirectExec(char const*, int) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x40416EA: pqxx::dbtransaction::do_exec(char const*) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x40618FA: pqxx::transaction_base::exec(std::string const&amp;, std::string const&amp;) (in /usr/lib/libpqxx-4.0.so) ==1647== by 0x80498F8: main (dbtest.cpp:21) </code></pre> <p>Any idea what's going on? It's very difficult to accept that a widely used library like libpqxx would have such a severe bug, so what might I be doing wrong here?</p> <p>Configuration details:</p> <ul> <li>OS: Linux 2.6.18-238.el5</li> <li>gcc version 4.4.0 </li> <li>libpqxx 4.0 </li> <li>postgres 9.2</li> </ul> <p>(<strong>Final edit: I found it easier to replace libpqxx with libpq than to keep investigating this memory leak.</strong>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload