I am attempting to interface with MySQL using the C API on an AMD64 machine. I am having problems with a segmentation fault after the first query is executed and results are returned.
Executing the queries 'directly' in MySQL is sucessful so I'm sure that I'm doing something totally, absolutely wrong with my code. I have Googled and studied code at codase.com but am not seeing what my code is missing. If anyone could point me in the right direction, I would appreciate it. Thanks in advance.
MySQL version 5.0.33 was built with the following options passed to the configure script:
--enable-thread-safe-client
--enable-assembler
--enable-local-infile
--without-debug
--without-bench
--without-readline
--with-extra-charsets=all
--with-embedded-server
Below are some snippets of my code to accompany this brief description of the program I am attempting to get working. The first snippet is the function that connects to the database. The connection is successful. Function 'initialize_system_tree' creates a GTK TreeView, then calls the function 'populate_system_tree' (the second snippet below).
The 'initialize_function_tree' function creates a second GTK TreeView, then calls the function 'populate_function_tree'. The seg fault occurs when attempting to execute the mysql_real_query.
Code:
void
db_connect()
{
...
else
{
fprintf(stderr, "Failed to connect to Database: Error: %s\n", mysql_error(&mysqlconn));
return FALSE;
}
initialize_system_tree();
initialize_function_tree();
return;
}
Code:
void
populate_system_tree()
{
GtkTreeIter iter, parent;
GtkTreePath *path;
gchar query[55];
MYSQL_RES *mysql_system_res;
MYSQL_ROW row;
...
sprintf(query, "SELECT * FROM SYSTEM ORDER BY IndentureLevel, ParentID");
mysql_real_query(&mysqlconn, query, (unsigned int)strlen(query));
mysql_system_res = mysql_store_result(&mysqlconn);
...
while (row = mysql_fetch_row(mysql_system_res))
{
...
}
sys_model = GTK_TREE_MODEL(sys_store);
gtk_tree_view_set_model(GTK_TREE_VIEW(sys_tree), sys_model);
g_object_unref(sys_model);
mysql_free_result(mysql_system_res);
gtk_tree_view_expand_all(GTK_TREE_VIEW(sys_tree));
return;
}
Code:
void
populate_function_tree()
{
...
gchar query[55];
MYSQL_RES *mysql_function_res;
MYSQL_ROW row;
sprintf(query, "SELECT * FROM FUNCTIONS ORDER BY FunctionID, ParentID");
mysql_real_query(&mysqlconn, query, (unsigned int)strlen(query));
...
}
[EDIT]Added the portion of the backtrace relative to MySQL...
Code:
#0 0x00002b291be6db4c in mysql_send_query (mysql=0x5096f8,
query=0x7fff8f0f8880 "SELECT * FROM FUNCTIONS ORDER BY FunctionID, ParentID", length=53) at client.c:2763
_db_func_ = 0x2b291bf47b0f "mysql_real_query"
_db_file_ = 0x2b291bf47a84 "client.c"
_db_level_ = 2
_db_framep_ = (char **) 0x7fff8f0f87b8
#1 0x00002b291be6dc17 in mysql_real_query (mysql=0x5096f8,
query=0x7fff8f0f8880 "SELECT * FROM FUNCTIONS ORDER BY FunctionID, ParentID", length=53) at client.c:2774
_db_func_ = 0x2b291bf478eb "?func"
_db_file_ = 0x2b291bf478f1 "?file"
_db_level_ = 1
_db_framep_ = (char **) 0x0
#2 0x00000000004069ab in populate_function_tree () at function_tree.c:165
iter = {stamp = 1, user_data = 0x0, user_data2 = 0x0,
user_data3 = 0x2b291e5a8e45}
parent = {stamp = 13236224, user_data = 0x637d80,
user_data2 = 0x406e00, user_data3 = 0x2b291e12bf3c}
path = <value optimized out>
query = "SELECT * FROM FUNCTIONS ORDER BY FunctionID, ParentID\000"
result = (MYSQL_RES *) 0x40
row = <value optimized out>