I'm writing an application that is driven mostly asynchronous with libev:
It's a socket server handling clients and it's listening on notifications.
For this I have two database connections con1 and con2 with con2 being asynchronous handling only the notifications.
I have declared PGresult *res; and use it pretty much everywhere with con1.
The problem arises when I receive a notification, I then run a query to see what's new or what's changed and use the results to send them to the socket clients.
This callback function, for the notification, has a local PGresult *res2; which I use (on con1) for this.
It then calls functions, in other files, to deliver these updates to the clients. These functions need information from the DB as well, using the global PGresult *res also on con1.
When I do PQclear(res) in the called function, the program terminates, no errors, no stacktrace, just stops. Removing that PQclear will allow returning the program to the callback function, where it then stops in the same way.
(The callback function is in a for loop using res2 at this point)
I have no idea why the program stops, any help would be appreciated.
Is it possible to define two seperate PGresults on one connection?
What do you mean by program terminates? Is it a segmentation fault? If so can you post gdb stack trace here?
As far as I know, on a single connection you can execute multiple statements but the result will reflect only the last executed statement. Maybe you can execute one statement copy the result to a local structure and then execute another one and use the local structure to access the previous result. PQclear frees the result structure and further accesses to that structure will ofcourse result in segmentation fault.
What I mean is that the logfile just ends with "PQclear(res)", nothing more.
No stacktrace, no memeory dump no segfault nothing that reveals anything.
But the process just doesn't exist anymore. (according to 'ps aux')
Meanwhile I have added, and removed #include <libpq-fe.h> in that file, and now I do get an error:
2009-09-09 13:11:47 function: PQclear(res)
*** glibc detected *** ./program: corrupted double-linked list: 0x092b09c8 ***
======= Backtrace: =========
P.S.: No problems in other functions in same file.
Are you sure there is no seg fault? Are you running it as a background process?
In any case, as I mentioned in my earlier post, you can try copying the result into a temporary structure and then using it later.