LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using C API; first query passed passed by program to MySQL is successful, second quer (https://www.linuxquestions.org/questions/programming-9/using-c-api%3B-first-query-passed-passed-by-program-to-mysql-is-successful-second-quer-560024/)

weibullguy 06-07-2007 04:16 PM

Using C API with MySQL 2nd query passed causes seg fault [solved]
 
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>


Mara 06-08-2007 03:45 PM

Are you sure you don't use any pointers to the result in your structures? It seems you're building something of the data.

weibullguy 06-12-2007 10:45 PM

Thanks for the tip, that got me looking in the right places.

I was using a structure as a "buffer" for the MySQL results. It was overlapping with the MySQL return structure resulting in weird data being passed to glibc functions. Don't really know why I was using the structure to "buffer" my results except I think structures are cool. In any event, populating the treeview directly with the results returned from the MySQL query eliminates the problem.


All times are GMT -5. The time now is 11:29 AM.