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

Ask A Question

Stats

  • Questions 223k
  • Answers 223k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make sure you are rebinding your data BEFORE you do… May 13, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer Typically you have a shield logo'd button and then shell… May 13, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer Unfortunately not. While the signature is a void *, it's… May 13, 2026 at 12:31 am

Related Questions

I have the following php code, which has been snipped for brevity. I am
I have the following code, which is the basis for pagination in a larger
I have a UINavigationController onto which I push a 'loading screen' UIViewController whilst I
Are there any alternatives to LogonUser and for impersonating given account in order to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.