The following bug has been logged on the website:
Bug reference: 7841
Logged by: Joanna Biernat-Gerono
Email address: biernatj@interia.pl
PostgreSQL version: 9.2.2
Operating system: Windows 7
Description: =
Sample code that provides to access violation (I am using the latest libpq
that is compiled with multithread support, connection was made to two
different dbs). The code is below:
#define WINNT_OS
#include <Windows.h>
#include <iostream>
#include "libpq-fe.h"
#include <signal.h>
#define STD__NULL NULL
#define STD__SUCCESS 0
#define STD__FAILED -1
#define STD__FAIL -1
#define TIMEOUT 100
using namespace std;
volatile bool m_bLicenseCheckedOut =3D false;
enum status
{
idle, =
running,
stop,
done
} m_running =3D idle;
int m_renewalTime =3D 0;
HANDLE m_hThread =3D NULL;
HANDLE m_hEvent =3D NULL;
DWORD WINAPI threadProc(LPVOID lpvoid)
{ =
int step =3D 0;
while(m_running =3D=3D running) { =
PGconn *conn;
=
try { =
const char *keys[]=3D{"dbname", "user", "password", "host", "port",
"sslcert", "sslkey", "sslrootcert", "sslmode", NULL};
const char *values[]=3D{"put_value_here", "put_value_here" ,
"put_value_here" , "put_value_here" , "put_value_here" , "put_value_here",
"put_value_here", "put_value_here" ,"verify-full", NULL};
=
// conn =3D PQconnectdbParams(keys,values,0);
//conn =3D PQsetdbLogin("put_value_here", "put_value_here", NULL,
NULL, "dbname=3Dput_value_here sslcert=3Dput_value_here sslkey=3Dput_value_=
here
sslrootcert=3Dput_value_here sslmode=3Dverify-full", "put_value_here" ,
"put_value_here");
conn =3D PQsetdbLogin("put_value_here", "put_value_here", NULL, NUL=
L,
"put_value_here", "put_value_here" , "put_value_here");
long status =3D PQstatus(conn);
=
if (status =3D=3D CONNECTION_OK) {
cout << "\nSuccessfully connected to DB:"<<step++ <<" ";
Sleep(1);
} else {
cout << "\nNot connected to ATR_DB";
cout << PQerrorMessage(conn);
}
} catch (...) {
cout << "Exception caught";
}
if(STD__NULL !=3D conn)
{
=
PQfinish(conn); =
conn =3D STD__NULL;
}
//wait before renewing again
// WaitForSingleObject(m_hEvent, TIMEOUT);
=
}
m_running =3D done;
return STD__SUCCESS;
}
/*
void handler(int sig) {
printf("Signal %d\n",sig);
}
*/
int DoSelect(int step)
{
PGconn *conn;
=
char conninfo[] =3D "dbname=3Dput_value_here host=3Dput_value_here
port=3Dput_value_here user=3Dput_value_here password=3Dput_value_here
sslmode=3Dverify-full sslcert=3Dput_value_here sslkey=3Dput_value_here
sslrootcert=3Dput_value_here" ;
// conn =3D PQsetdbLogin("put_value_here", "put_value_here", NULL, NULL,
"dbname=3Dput_value_here sslmode=3Dverify-full sslcert=3Dput_value_here
sslkey=3Dput_value_here sslrootcert=3Dput_value_here", "put_value_here",
"put_value_here");
conn =3D PQsetdbLogin("put_value_here", "put_value_here", NULL, NULL,
"put_value_here", "put_value_here", "put_value_here");
// conn =3D PQconnectdb(conninfo);
// long status =3D 1; =
long status =3D PQstatus(conn);
int r =3D PQisthreadsafe();
if (status =3D=3D CONNECTION_OK)
{
cout << "\nSuccesfully connected to MOTOLM DB: "<< step <<" \n";
} else {
cout << "\nNot connected to MOTOLM DB\n";
cout << PQerrorMessage(conn);
}
if(STD__NULL !=3D conn)
{
=
PQfinish(conn); =
conn =3D STD__NULL;
}
return 0;
}
int main (int argc,char** args)
{
// int ts =3D PQisthreadsafe();
// cout << "PQisthreadsafe =3D " << ts;
//typedef void (*SignalHandlerPointer)(int);
//SignalHandlerPointer previousHandler;
//previousHandler =3D signal(SIGSEGV, handler); // install our handler
cout << "PROGRAM STARTED"<< endl;
m_hEvent =3D CreateEvent(0,false,0,0);
if (NULL =3D=3D m_hEvent)
{
cout << "CreateEvent FAILED" << endl; =
return STD__FAILED;
}
m_running =3D running;
m_hThread =3D CreateThread(NULL, 0, threadProc, 0,0, NULL);
if (NULL =3D=3D m_hThread)
{
m_running =3D done;
cout << "CreateThread FAILED" << endl; =
return STD__FAILED;
}
if( FALSE =3D=3D SetThreadPriority(m_hThread,
THREAD_PRIORITY_TIME_CRITICAL))
{
cout << "SetThreadPriority FAILED" << endl; =
return STD__FAILED;
}
=
int i =3D 0;
while (i < 5000) {
=
DoSelect(i++);
Sleep(1);
}
m_running =3D stop;
cout << "done." << endl;
return STD__SUCCESS;
}