2012-10-12 27 views
0

我有以下功能:mysql_init(NULL)分段錯誤

int get_tcounter(char *meterType, int *error) { 
    MYSQL *conn = mysql_init(NULL); 
    get_connection(conn, DB_HOST, DB_USER, DB_PW, DB); 

    MYSQL_STMT *stmt = mysql_stmt_init(conn); 
    if (!stmt) { 
     log_to_console("Init stmt failed: %s\n", mysql_error(conn)); 
     exit(1); 
    } 
    prepare_stmt(conn, stmt, GET_TCOUNTER_SQL); 

    MYSQL_BIND param[1], res[1]; 
    memset(param, 0, sizeof(param)); 
    memset(res, 0, sizeof(res)); 

    char *paramBuff; 
    int tcBuff; 
    my_bool isNullParam[] = {0}, isNullRes[] = {0}; 

    paramBuff = meterType; 

    bind_string_param(&param[0], strlen(meterType), paramBuff, &isNullParam[0], (unsigned long *) 0); 
    bind_result(&res[0], MYSQL_TYPE_LONG, (char *) &tcBuff, &isNullRes[0], (unsigned long *) 0); 

    execute_statement(stmt, param, res); 

    *error = TRUE; 
    int ret = 0; 
    if (!mysql_stmt_fetch(stmt)) { 
     ret = tcBuff; 
     *error = FALSE; 
    } 

    mysql_stmt_close(stmt); 
    mysql_close(conn); 

    return ret; 
} 

我第一次執行所有工作正常功能,但第二次左右,mysql_init行拋出段錯誤。

這裏的核心轉儲:

Program terminated with signal 11, Segmentation fault. 
#0 0x00007fe23ee22f08 in ??() from /lib/x86_64-linux-gnu/libc.so.6 
(gdb) down 
Bottom (innermost) frame selected; you cannot go down. 
(gdb) up 
#1 0x00007fe23ee24536 in ??() from /lib/x86_64-linux-gnu/libc.so.6 
(gdb) up 
#2 0x00007fe23ee270b5 in malloc() from /lib/x86_64-linux-gnu/libc.so.6 
(gdb) up 
#3 0x00007fe23fa951d2 in my_malloc() from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 
(gdb) up 
#4 0x00007fe23fa788df in mysql_init() from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 
(gdb) up 
#5 0x0000000000403ed4 in get_tcounter (meterType=0x24bcbd0 "M-9669-8", error=0x7fff6b57a8a8) at database/data_access.c:284 
284  MYSQL *conn = mysql_init(NULL); 

奇怪的是,在第一次調用conn爲0x0初始化之前,但第二次它的一些任意0x7FFF的...地址。

綁定爲conn分配NULL並在init之前釋放conn無濟於事。

回答

0

這是一個很好的機會,這是在MySQL中設置一些不良狀態的內存損壞。嘗試在Valgrind下運行,看看是否有雙重free或某些類型的無效訪問。

-1

不要鏈接調試庫(libmysqld.lib),我測試它在win10 + mysql5.6 5.7 + vs2017,mysql_init總是返回NULL或崩潰。你應該鏈接發佈庫(libmysql.lib)。

+0

這個問題似乎與Linux有關,所以我認爲Windows解決方案在這裏用處不大。 –