【发布时间】:2014-11-10 07:40:43
【问题描述】:
嗯,我已经在这个小项目上工作了一周左右,作为我参加 C 课程的学习经验。到目前为止,我几乎没有错误地工作过,我被指示将 *con 设为结构,因为最后我得到了一个巨大的堆栈转储(这是由指针问题引起的)
我的程序运行良好,一切正常。但是,在关闭 mysql 连接后,我遇到了段错误。
代码:
#include <my_global.h>
#include <mysql.h>
#include <string.h>
#include <stdlib.h>
struct sql {
MYSQL *con;
};
int try(char getInput[100]) {
struct sql grab;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "iluvgeordi";
char *database = "test";
if( strcmp( getInput, "version" ) == 0 )
printf( "\n->Mysql Version: %s\n", mysql_get_client_info() );
else if( strcmp( getInput, "get" ) == 0 ) {
grab.con = mysql_init(NULL);
if( !mysql_real_connect(grab.con, server, user, password, database, 0, NULL, 0) ) {
fprintf(stderr, "%s\n", mysql_error(grab.con));
exit(1);
}
if( mysql_query(grab.con, "show tables") ) {
fprintf(stderr, "%s\n", mysql_error(grab.con));
exit(1);
}
res = mysql_use_result(grab.con);
if( res != NULL ) {
while( ( row = mysql_fetch_row(res) ) != NULL )
printf( "%s \n", row[0] );
}
mysql_free_result(res);
}
mysql_close(grab.con);
}
虽然我知道它是 mysql_close,但这里是 GDB 堆栈转储/回溯
程序收到信号SIGSEGV,分段错误。 0xf7cca37e 在 mysql_close() 来自 /usr/lib/i386-linux-gnu/libmysqlclient.so.18 (gdb) 在哪里
0 0xf7cca37e in mysql_close() from /usr/lib/i386-linux-gnu/libmysqlclient.so.18
1 0x08048afa in try (getInput=0xffffd37c "version") at m-try.c:61
2 0x0804894b in main (argc=1, argv=0xffffd494) at main.c:37 (gdb) bt
0 0xf7cca37e in mysql_close() from /usr/lib/i386-linux-gnu/libmysqlclient.so.18
1 0x08048afa in try (getInput=0xffffd37c "version") at m-try.c:61
2 0x0804894b in main (argc=1, argv=0xffffd494) at main.c:37 (gdb) 退出
我知道有 1000 个 mysql C 问题,但是它们都与我需要的东西无关,否则我现在肯定会找到它。
【问题讨论】: