Thread: libpd - PQfinish() - crashes client application in some cases
I shall try to write english well enough :)
I'm writing a GUI client to access my PostrgeSQL DB, with FLTK libraries. Sometimes, the client crashes.
I have written un source code, as little as possible, without FLTK libraries, to test the behavior of PQfinish(), and I noticed :
If no connexion has ever been made, [ PQstatus() retuns 'CONNECTION_BAD' ] , a call to PQfinish(...) does not do something wrong.
But il I connect with PQsetdblogin(..), [ PQstatus returns 'CONNECTION_OK ], a first call to PQfinish(...) disconnects the client, [ PQstatus returns 'CONNECTION_BAD' ], and A SECOND call to PQfinish(..) make the client to crash with a system message 'segmentation fault'.
The source code I made to test interactively connections, disconnections and status is attached to this mail.
I could be told that one does not call PQfinish() when PQstatus is 'CONNECTION_BAD, but it is done in the PostgreSQL documentation example (Programmer's guide, chapter 1.11)
I hope this will be useful for you.
Here is my computer configuration :
CPU : Pentium II 350 MHz
OS : Linux SuSE 7.2 upgraded with kernel 2.4.8
Compiler : GNU gcc version2.95.3
Linker : GNU ld version 2.10.91 (with BFD 2.10.91.0.4)
PostgreSQL : Version 7.1.3 (downloaded on ftp.fr.postgresql.org )
-- Jean-Michel Chabanne 77450 MONTRY (FRANCE) 48" 54' N - 2" 49' E Powered by Linux
"Jean-Michel.chabanne" <jeanmichel.chabanne@free.fr> writes: > ... a first call to PQfinish(...) disconnects the client, > [ PQstatus returns 'CONNECTION_BAD' ], and A SECOND call to PQfinish(..) > make the client to crash with a system message 'segmentation fault'. Freeing the same chunk of memory twice leads to a crash in most implementations of malloc/free :-(. I don't think that libpq can be expected to protect you from this. Don't PQfinish() the same connection object more than once, because after the first one it's not a connection object anymore --- only a dangling pointer to freed memory. For that matter, you shouldn't call PQstatus() or any other libpq function using an already-PQfinished connection pointer. regards, tom lane