List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:May 11 1999 12:18pm
Subject:Seg fault in c api
View as plain text  
>>>>> "Dustin" == Dustin Machi <dmachi@stripped> writes:

Dustin> I'm going crazy trying to figure out where I am getting a segv in
Dustin> a program that i'm writing that accesses a mysql db. I'm using version
Dustin> 3.22.22 mysql. I have had the program working to where it would perform
Dustin> a single query. After this query, there is another I'd like to perform,
Dustin> however whenever I do mysql_free_result(&server_db) i get a segv, or
Dustin> if i don't free the result, I get a fault when i do mysql_query. I can
Dustin> see that the server is getting the query in my mysql logs, but as soon as
Dustin> it shows up in the logs my program crashes. Here is a snipet of the code
Dustin> that Im referring to:

Dustin> /* this is the part of the query that _does_ work */

Dustin>   if (!mysql_query(&server_db, sql)) {
Dustin>       result = mysql_store_result(&server_db);

Dustin>       if ((numsystems = mysql_num_rows(result)) > 0) {
Dustin>          sprintf(buf, "%d Systems retrieved from the server.",
Dustin> numsystems);
Dustin>          log(buf);

Dustin>          while ( (row = mysql_fetch_row(result)) ) {
Dustin>             insert_system(atoi(row[0]), row[1], atof(row[2]),
Dustin> atof(row[3]), atof(row[4]));

Dustin>          }
Dustin>       } else {
Dustin>          log("No systems found on server...aborting");
Dustin>          return FALSE;
Dustin>       }

Dustin>    } else {
Dustin>       log("Error retrieving Systems from server...aborting");
Dustin>       return FALSE;
Dustin>    }

Dustin> /* when i free the results right here, she croaks on me */

Dustin>    mysql_free_result(result);

Hi!

Note that if mysql_query() didn't work, you will free a random
pointer!

Fix: move mysql_free_result() after the mysql_fetch_row() loop.  Note 
     that you must also add a call to mysql_free_result() before the
     log("No systems found on server...aborting");
     statement!

Please take also look at the MySQL manual section
'Debugging a MySQL client'.  This includes some tips how you can debug 
the above!

Regards,
Monty
Thread
Seg fault in c apiDustin Machi8 May
  • Seg fault in c apiMichael Widenius11 May