I have this weird behavior in C which I do not understand. It is probably me as I am fairly new to C. What I am trying to achieve is to write a function that runs a MySQL query and returns the result. The weird thing is that, in the code below, if I close the connection and free the result (as I would expect the order to be) it seems that the value that I have stored in 'retvalue' is no longer there. When I put the return before the freeing of the result and closing of the connection the value is returned, but obviously the connection is not closed and MySQL will eventually present the error 'too many connections'.I have this weird behavior in C which I do not