2 messages in com.mysql.lists.plusplusMysql++ losing a connection bug
FromSent OnAttachments
Jason Kushmaul24 Jul 2002 08:26 
Jason Kushmaul29 Jul 2002 09:47 
Subject:Mysql++ losing a connection bug
From:Jason Kushmaul (jkus@wirelessdeveloper.com)
Date:07/24/2002 08:26:44 AM
List:com.mysql.lists.plusplus

My program crashes with a segfault within the mysql++ api or within the mysql client c api which is being called from the c++ api.

Below is the source code for my test app and the gdb output of the binary files core file.

Compile test.cpp run the binary seperate shell: mysqladmin shutdown -u root -p <pass> Let it shutdown, and watch the error messages scroll from my exception catch. But you can also see that dbConn.connected() returns true, because "Hi steve" shows up. The only way that "hi steve" can get output is if(dbConn.connected()){}

start the server back up /etc/rc.d/init.d/mysqld start (just start the mysql daemon however you go about that...)

BAM, it segfaults within the api.

So, Connection for some reason does not know that it has lost its connection, and thus I'm executing queries when there isn't even a connection even though I'm testing for that...

The reason for the segfault is obviously because I'm inside all of the Query and Result code when I should not be, which I could prevent if(dbConn.connected) returned false...

----------Source Code of test.cpp---------------------- #include <iostream.h> #include <sqlplus.hh> #include <unistd.h> #include <string> #include <time.h> #include <stdio.h> #include <signal.h>

int cleanupAndExit=0; int reload=0; char *lastdebug=0; unsigned long debugRpt=0; unsigned long newMsg=0; pthread_mutex_t debug_mutex;

void debugOut(const char *msg) {

pthread_mutex_lock(&debug_mutex); printf("%s\n",msg);fflush(stdout); pthread_mutex_unlock(&debug_mutex);

}

void Signal_Handler(int sig) /* signal handler function */ {

#ifdef DEBUG debugOut("Shutting down daemon immediatly"); #endif cleanupAndExit=1;

}

int main(int argc, char **argv) {

pthread_mutex_init(&debug_mutex,NULL);

/**Send this process into daemon mode**/ int err=0;

Connection dbConn(use_exceptions);//("BroadcastInfo","localhost","broadcastinfo","fahq");

while(!cleanupAndExit){ try{ if(!dbConn.connected()){ dbConn.connect("BroadcastInfo","localhost","broadcastinfo","fahq"); } else{ debugOut("hi steve"); } while(!cleanupAndExit&&dbConn.connected()){ if(!dbConn.connected()){

dbConn.connect("BroadcastInfo","localhost","broadcastinfo","fahq"); } else{ debugOut("hi steve x 2"); } Query bQuery = dbConn.query(); bQuery<<"SELECT CURDATE() AS CurDate,CURTIME() AS CurTime"; Result res=bQuery.store(); Row bjs; Result::iterator i;

for (i=res.begin();i!=res.end();i++){ bjs=*i; } } } catch(BadQuery er){ string errmsg("main() - Error: "); errmsg+=string(er.error); debugOut(errmsg.c_str()); err= -1; } catch(BadConversion er){ string errmsg("main() - Error: Tried to convert \""); errmsg+=string(er.data); errmsg+=string("\" to a \""); errmsg+=string(er.type_name); debugOut(errmsg.c_str()); err= -1; } }

mainDie:

pthread_mutex_destroy(&debug_mutex);

return err; }

-----------------End of test.cpp---------------

----------------gdb results------------------------------------------------ (gdb) bt #0 0x4023a4a1 in malloc () from /lib/libc.so.6 #1 0x4023a20a in malloc () from /lib/libc.so.6 #2 0x402f7215 in my_strdup () from /usr/local/mysql/lib/mysql/libmysqlclient.so.11 #3 0x402f27ff in mysql_real_connect () from /usr/local/mysql/lib/mysql/libmysqlclient.so.11 #4 0x402f2c2b in mysql_reconnect () from /usr/local/mysql/lib/mysql/libmysqlclient.so.11 #5 0x402f0931 in simple_command () from /usr/local/mysql/lib/mysql/libmysqlclient.so.11 #6 0x402f353f in mysql_send_query () from /usr/local/mysql/lib/mysql/libmysqlclient.so.11 #7 0x402f4514 in mysql_real_query () from /usr/local/mysql/lib/mysql/libmysqlclient.so.11 #8 0x402f44db in mysql_query () from /usr/local/mysql/lib/mysql/libmysqlclient.so.11 #9 0x40027c7a in MysqlConnection::store (this=0x400607f0, str=@0xbffff2e0, throw_excptns=true) at /usr/include/g++-3/std/bastring.h:343 #10 0x0804fabf in MysqlConnection::store (this=0xbffff530, str=@0xbffff2e0) at /usr/local/include/connection1.hh:92 #11 0x0804dbf8 in MysqlQuery::store (this=0xbffff410, p=@0xbffff4ec, r=RESET_QUERY) at /usr/local/include/query2.hh:39 #12 0x0804d72f in MysqlQuery::store (this=0xbffff410, r=RESET_QUERY) at /usr/local/include/query1.hh:74 #13 0x0804bc24 in main (argc=1, argv=0xbffff7c4) at test.cpp:70 #14 0x401d90de in __libc_start_main () from /lib/libc.so.6 (gdb)

-------------------------End of gdb results-----------------------------------

Become a Member Today!! http://www.WirelessDeveloper.com