Is mysql_close required when connection fails?

I have a piece of code that connects to such a MySQL database (not directly from the code, so there may be typos):

m_connectionHandler = mysql_init(NULL);
if (m_connectionHandler == NULL)
{
  // MySQL initialization failed
  return;
}

MYSQL *tmp = mysql_real_connect(m_connectionHandler,
                                m_hostname,
                                m_username,
                                m_password,
                                m_dbName,
                                m_port,
                                NULL,
                                m_flags);
if (tmp == NULL)
{
  // Connect failed
  mysql_close(m_connectionHandler);
  return;
}

My question is: if mysql_close(in the second case, if tmp == NULL), in the case when it mysql_real_connectreturns NULL, is it required, or if mysql_real_connectthe connection handler frees me for an error?

The documentation states that what you get from mysql_initshould be freed mysql_close, but there are signs that it is already freed mysql_real_connectupon failure.

Can anyone shed some light on this for me?

+3
source share
1 answer

mysql_init(NULL) . , , , mysql_close, , . , mysql_real_connect .

+3

Source: https://habr.com/ru/post/1717344/


All Articles