IndependentProgrammer
IndependentProgrammer

Reputation: 28

SegFaults in C/MySQL through Structured Con

Well, i've been working on this little project for a week or so now as a learning experience as I take C courses. I've worked it almost error free until now, I was instructed to make *con a struct because I was getting a huge stack dump at the end (which was caused by a pointer problem)

My program works fine, everything. However upon closing the mysql connection I am getting a segfault.

Code:

#include <my_global.h>
#include <mysql.h>
#include <string.h>
#include <stdlib.h>
struct sql {

    MYSQL *con;

};

int try(char getInput[100]) {

   struct sql grab;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server = "localhost";
   char *user = "root";
   char *password = "iluvgeordi"; 
   char *database = "test";

    if( strcmp( getInput, "version" ) == 0 )
        printf( "\n->Mysql Version: %s\n", mysql_get_client_info() );
    else if( strcmp( getInput, "get" ) == 0 ) {
    
        grab.con = mysql_init(NULL);
        
        if( !mysql_real_connect(grab.con, server, user, password, database, 0, NULL, 0) ) {
        
            fprintf(stderr, "%s\n", mysql_error(grab.con));
            exit(1);
        
        }
        
        if( mysql_query(grab.con, "show tables") ) {
            
            fprintf(stderr, "%s\n", mysql_error(grab.con));
            exit(1);

        }
            
        res = mysql_use_result(grab.con);
        if( res != NULL ) {
            
            while( ( row = mysql_fetch_row(res) ) != NULL )
                printf( "%s \n", row[0] );
            
        }
        mysql_free_result(res); 
    
    }

    mysql_close(grab.con);
    
}

Even though I know its the mysql_close, here the GDB Stack Dump/Back Trace

Program received signal SIGSEGV, Segmentation fault. 0xf7cca37e in mysql_close () from /usr/lib/i386-linux-gnu/libmysqlclient.so.18 (gdb) where

0 0xf7cca37e in mysql_close () from /usr/lib/i386-linux-gnu/libmysqlclient.so.18

1 0x08048afa in try (getInput=0xffffd37c "version") at m-try.c:61

2 0x0804894b in main (argc=1, argv=0xffffd494) at main.c:37 (gdb) bt

0 0xf7cca37e in mysql_close () from /usr/lib/i386-linux-gnu/libmysqlclient.so.18

1 0x08048afa in try (getInput=0xffffd37c "version") at m-try.c:61

2 0x0804894b in main (argc=1, argv=0xffffd494) at main.c:37 (gdb) quit

I know there is 1000's of mysql C questions, however none of them are relavent to what I need otherwise I surely would have found it by now.

Upvotes: 0

Views: 134

Answers (1)

Ingo Leonhardt
Ingo Leonhardt

Reputation: 9904

It seems that error is caused by a call of try( "version" );:

1 0x08048afa in try (getInput=0xffffd37c "version") at m-try.c:61

and indeed, you only all mysql_init() and assign a value to grab.con if you call try( "get" ) but you always call mysql_close( grab.con );

You should just put that call into the "get" branch;

....
else if( strcmp( getInput, "get" ) == 0 ) {
    ...
    mysql_close( grab.con );
}

Upvotes: 1

Related Questions