我正在尝试使用C API查询mysql 5.5。
dbutil.c 包含样板代码:
#include "../include/dbutil.h"
#include "../include/logging.h"
#include "../include/common.h"
MYSQL get_connection(char *host, char *user, char *passwd, char *db) {
MYSQL conn;
mysql_init(&conn);
if (!mysql_real_connect(&conn, host, user, passwd, db, 0, NULL, 0)) {
log_to_console("Cannot connect to MySQL server: %s", mysql_error(&conn));
exit(1);
}
return conn;
}
MYSQL_STMT prepare_stmt(MYSQL *conn, char *sql) {
MYSQL_STMT *stmtP = mysql_stmt_init(conn);
if (!stmtP) {
log_to_console("Cannot create statement, out of memory.");
exit(1);
}
MYSQL_STMT stmt = *stmtP;
if (mysql_stmt_prepare(&stmt, sql, strlen(sql))) {
log_to_console("Preparing statement failed: %s", mysql_error(conn));
exit(1);
}
return stmt;
}
void bind_param(MYSQL_BIND *param, enum enum_field_types fieldType, char* buffer, my_bool *isNull, unsigned long *length) {
param->buffer_type = fieldType;
param->buffer = buffer;
param->is_null = isNull;
param->length = length;
}
void bind_string_param(MYSQL_BIND *param, int stringSize, char* buffer, my_bool *isNull, unsigned long *length) {
bind_param(param, MYSQL_TYPE_STRING, buffer, isNull, length);
param->buffer_length = stringSize;
}
void bind_result(MYSQL_BIND *result, enum enum_field_types fieldType, char* buffer, my_bool *isNull, unsigned long *length) {
bind_param(result, fieldType, buffer, isNull, length);
}
void bind_string_result(MYSQL_BIND *result, int stringSize, char* buffer, my_bool *isNull, unsigned long *length) {
bind_string_param(result, stringSize, buffer, isNull, length);
}
下面是实际运行查询的代码:
#include "../include/dbutil.h"
int main() {
MYSQL conn = get_connection("localhost", "user", "pass", "test");
MYSQL_STMT stmt = prepare_stmt(&conn, "SELECT hostname from hosts where id = ?");
MYSQL_BIND param[1], result[1];
memset(param, 0, sizeof(param));
memset(result, 0, sizeof(result));
int in = 1;
unsigned long length = 200;
my_bool isNull[2];
isNull[0] = 0;
char out[200];
bind_param(¶m[0], MYSQL_TYPE_LONG, (char *) &in, &isNull[0], (unsigned long *)0);
bind_string_result(&result[0], 200, (char *) &out, &isNull[1], &length);
mysql_stmt_bind_param(&stmt, param);
mysql_stmt_execute(&stmt);
mysql_stmt_bind_result(&stmt, result);
mysql_stmt_store_result(&stmt);
mysql_stmt_fetch(&stmt);
printf("Restult %sn", out);
mysql_stmt_close(&stmt);
mysql_close(&conn);
return 0;
}
语句执行并返回预期结果,但代码在命中时出现段错误
mysql_stmt_close(&stmt);
我已经浏览了核心转储,发现段错误是由libc free()语句引起的,我认为该语句是在libmysql.c的free_root()中调用的。
my_bool STDCALL mysql_stmt_close(MYSQL_STMT *stmt)
{
MYSQL *mysql= stmt->mysql;
int rc= 0;
DBUG_ENTER("mysql_stmt_close");
free_root(&stmt->result.alloc, MYF(0));
free_root(&stmt->mem_root, MYF(0));
free_root(&stmt->extension->fields_mem_root, MYF(0));
...
具体来说。核心转储表示 *stmt.extension 是发生语句关闭的时间。任何想法为什么会这样?
听起来代码/库正在尝试free()
一些已经发布的内存。
经过一番谷歌搜索,我找到了这个链接。在调用 mysql_stmt_close()
之前,您似乎应该检查以确保stmt
不是 NULL。如果stmt
已经为 NULL,则库已经清理了它,无需调用 mysql_stmt_close()
。
在此处查看 out 参数:
char out[200];
...
bind_string_result(&result[0], 200, (char *) &out, &isNull[1], &length);
如果您看到bind_string_result声明:
void bind_string_result(MYSQL_BIND *result, int stringSize, char* buffer, my_bool *isNull, unsigned long *length) {
该函数期望在您发送指向char *
的指针时接收 char *
类型的参数,换句话说,char **
编译器不会给您任何警告,因为您要将参数强制转换为char *
但它可能会在内存中产生冲突。 尝试调用函数为:
bind_string_result(&result[0], 200, out, &isNull[1], &length);