tags:

views:

39

answers:

2

I'm working on a system using lots of MySQL queries and I'm running into some memory problems I'm pretty sure have to do with me not handling pointers right...

Basically, I've got something like this:

MYSQL_ROW function1() {
   string query="SELECT * FROM table limit 1;";
   MYSQL_ROW return_row;

   mysql_init(&connection); // "connection" is a global variable
   if (mysql_real_connect(&connection,HOST,USER,PASS,DB,0,NULL,0)){
      if (mysql_query(&connection,query.c_str()))
         cout << "Error: " << mysql_error(&connection);
      else{
         resp = mysql_store_result(&connection); //"resp" is also global
        if (resp) return_row = mysql_fetch_row(resp);
        mysql_free_result(resp);
      }
      mysql_close(&connection);
   }else{
      cout << "connection failed\n";
      if (mysql_errno(&connection))
         cout << "Error: " << mysql_errno(&connection) << " " << mysql_error(&connection);
   }
   return return_row;
}

And function2():

MYSQL_ROW function2(MYSQL_ROW row) {
    string query = "select * from table2 where code = '" + string(row[2]) + "'";
    MYSQL_ROW retorno;

    mysql_init(&connection);
    if (mysql_real_connect(&connection,HOST,USER,PASS,DB,0,NULL,0)){
        if (mysql_query(&connection,query.c_str()))
            cout << "Error: " << mysql_error(&conexao);
        else{
            // My "debugging" shows me at this point `row[2]` is already fubar
            resp = mysql_store_result(&connection);
            if (resp) return_row = mysql_fetch_row(resp);
            mysql_free_result(resp);
        }
        mysql_close(&connection);
    }else{
        cout << "connection failed\n";
        if (mysql_errno(&connection))
        cout << "Error : " << mysql_errno(&connection) << " " << mysql_error(&connection);
    }
    return return_row;
}

And main() is an infinite loop basically like this:

int main( int argc, char* args[] ){
    MYSQL_ROW row = NULL;
    while (1) {
        row = function1();
        if(row != NULL) function2(row);
   }
}

(variable and function names have been generalized to protect the innocent)

But after the 3rd or 4th call to function2, that only uses row for reading, row starts losing its value coming to a segfault error...

Anyone's got any ideas why? I'm not sure the amount of global variables in this code is any good, but I didn't design it and only got until tomorrow to fix and finish it, so workarounds are welcome!

Thanks!

A: 

You should not be closing the connection or freeing the result set while you are processing a row returned by mysql_fetch_row.

anon
Even if I save the result in a variable before closing? Because that's the intent here... I'm not sure I'm doing it right, though...
Gabe
+1  A: 

Update: I misunderstood how mysql results are used. It looks like the row pointer array points to the results array which you free in function1() and then use it in function2() after it has been returned to the heap.

What you need to do is copy return_row[2] to a persistent string before freeing the results. Then pass that on to function2(). I see you doing something similar in function2() so you need to fix it there as well (though in your example you aren't doing anything with its return value).

Also, you are correct that free(row); is not the correct thing to do.

Amardeep
I tried free(row) at the end of the loop, but I'm guessing that's terribly wrong, right? Thanks
Gabe
@Gabe: See my revision above.
Amardeep
Thanks, but `row` has actually 5 fields I need to store in order to do other stuff. Sorry if I didn't make that clear. It's got 5 fields and I could store the strings no problem, but if I could somehow copy and save the entire `mysql_fetch_row(resp)` result into `row`, that would be ideal
Gabe
`row` is not a copy of the results data. It is just an array of pointers to the existing results data. If you need to use the `resp` data just don't free it until after you are done using it.
Amardeep