Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 363463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:20:42+00:00 2026-05-12T13:20:42+00:00

I have a code snippet which connects to a MySQL database like this (not

  • 0

I have a code snippet which connects to a MySQL database like this (not directly from code so there might 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 if clause tmp == NULL), in the case when mysql_real_connect returns NULL, is required, or if mysql_real_connect frees the connection handler for me upon failure?

The documentation does state that what you get from mysql_init should be freed by mysql_close, but there are indications that it’s already freed by mysql_real_connect upon failure.

Can anyone shed some light on this for me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:20:42+00:00Added an answer on May 12, 2026 at 1:20 pm

    Your call to mysql_init(NULL) allocates memory. Regardless of whether you’re able to really connect to the server, you’ve still allocated memory, so you need to free it with mysql_close, which not only closes connections but also releases memory. I see no indication in the documentation that mysql_real_connect would free the memory itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code snippet, which is like this... #import <sqlite3.h> @interface DatabaseClass : NSObject
Say, I have a code snippet like this: public static void main(String[] args) {
I have written a code snippet which i definitely feel its not written a
I have this code snippet: <div id=div1> </div> <div id=div2> <h3>This is the content</h3>
I have this code snippet inside a function that checks if an object exists
VS 2008 I have this code snippet I found on a VB website. But
Suppose i have the code snippet as follows : ( clarification purpose/not well formed
In the iPhone Push Notification documentation, they have a code snippet in which they
I have a code snippet in QML which should look for the regexp Calling
I came across this strange code snippet which compiles fine: class Car { public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.