I have a trouble with application when i try to connect (PQconnectdb) with database Postgresql. When i don’t invoke any function after connect with database everything is OK, i can communicate with database. If, for example i invoke function ‘MainApp.Init()’ after connect, application hang (segmentation fault).
What’s wrong.
With debug gdb, gdb return error Thread 1 “CameraModule” received signal SIGSEGV, Segmentation fault.
0x0000007fb6e92918 in strcmp () from /lib/aarch64-linux-gnu/libc.so.6
example code:
…
LOGINFO(“PQconnectdb\n”);
ConnectionPtr pConnection = PQconnectdb( szconn.c_str() ); //connect with database
MainApp.Init() //segmentation fault when invoke this function
Hi, I use Postgressql from c library. Used functions posgressql compiled in C mixed with Go.
Problem is only why application hangs after connect, when i invoke some function in Go after connect.
When i dont’ call any functions after connect everything is ok.
Because of compatibility in code with other applications i must use interface with Postgressql in C.
Weird, when i call some functions in Go application hang, only after connect with database.Why when i don’t call any function in Go application ok works? I tried use mutexes in Go while i try to connect, and error also.
//----------------------------------------------------------------------
//code in Go calling C
func InitConnections(uri string) {
C.InitCPanelInterface(C.CString(uri), C.int(5))
}
//here code in C
void InitCPanelInterface( const char* connString, int size )
{
using namespace ltm;
ConnectionPool* pConn = ConnectionPool::instance();
InitializationFunc func( &CPanelQueries::init );
pConn->init( connString, size, func );
}
void ConnectionPool :: init( const std::string& szconn, unsigned int size,
InitializationFunc func )
{
using namespace std;
So the C part works as long as the Go code does not call any C functions after a database connection is made?
You mention mutexes, so I assume your Go code runs multiple goroutines. The libpq docs say that libpq may or may not be thread-safe, depending on the compile options used. If you call PQisthreadsafe(), does it return a 1?
I also wonder about the SEGV in strcmp(), which seems to indicate that a string is not properly null-terminated. (But OTHO, c_str() should ensure that, correct?) Is uri a valid connection string, and does it survive all the transformations (C.Cstring(), c_str()) it goes through?
Strange, i have added parameter ‘sslmode=disable’ in function ‘PQconnectdb’ and now is working Ok!
PQconnectdb(“user=‘cam’ password=‘cam’ host=‘127.0.0.1’ dbname=‘cpanel’ port=‘5432’ sslmode=disable”).
Thanks for help.