简体   繁体   English

mysql_init(NULL)分段错误

[英]mysql_init(NULL) segmentation fault

I have the following function: 我有以下功能:

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;
}

First time I execute the function all is working as expected, but the second time around, mysql_init line throws Segmentation Fault. 第一次执行该功能时,一切都按预期工作,但是第二次, mysql_init行抛出了Segmentation Fault。

Here's the core dump: 这是核心转储:

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);

The strange thing is that in first call conn is 0x0 before init, but the second time it's some arbitrary 0x7fff... address. 奇怪的是,在第一次调用conn之前,init是0x0,但是第二次调用时是任意的0x7fff ...地址。

Tied assigning NULL to conn and freeing conn before init to no avail. 绑定为conn分配NULL并在初始化前释放conn无济于事。

There's a good chance this is memory corruption that sets up some bad state in MySQL. 这很有可能是内存损坏,从而在MySQL中设置了一些错误状态。 Try running this under Valgrind to see if there is a double free or invalid access of some kind. 尝试在Valgrind下运行此命令,以查看是否存在某种形式的双重free或无效访问。

Don't link debug library(libmysqld.lib), i test it on win10+mysql5.6 5.7+vs2017, mysql_init always return NULL or crash. 不要链接调试库(libmysqld.lib),我在win10 + mysql5.6 5.7 + vs2017上对其进行测试,mysql_init总是返回NULL或崩溃。 You should link release library(libmysql.lib). 您应该链接发布库(libmysql.lib)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM