LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-07-2007, 04:16 PM   #1
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,692
Blog Entries: 1

Rep: Reputation: 217Reputation: 217Reputation: 217
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>

Last edited by weibullguy; 06-12-2007 at 10:46 PM.
 
Old 06-08-2007, 03:45 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,533

Rep: Reputation: 148Reputation: 148
Are you sure you don't use any pointers to the result in your structures? It seems you're building something of the data.
 
Old 06-12-2007, 10:45 PM   #3
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,692
Blog Entries: 1

Original Poster
Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passed RHCT ~=gr3p=~ Linux - Certification 25 04-12-2009 10:38 AM
Cannot get passed 'make' for Superkaramba xstealthrtx Linux - Newbie 11 11-29-2004 05:35 AM
Passed LPI 102 :D Blinker_Fluid Linux - Certification 2 10-20-2004 05:43 PM
I passed LPI102. Yay! Q*Bert Linux - General 4 03-10-2003 12:16 PM
php variables not being passed on tpat Linux - Software 1 01-11-2003 03:11 PM


All times are GMT -5. The time now is 03:26 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration