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

  • SEARCH
  • Home
  • 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 9240147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:08:21+00:00 2026-06-18T08:08:21+00:00

I am writing a daemon in C++, it has a simple task of inserting

  • 0

I am writing a daemon in C++, it has a simple task of inserting some events into a mysql database.

When I ran the top command I saw that the processe’s memory needs increase, I thought I had a memory leak and I started using Valgrind

I ran valgrind as this:

valgrind –tool=memcheck –leak-check=yes –show-reachable=yes
–num-callers=20 –track-fds=yes ./my_app

I get the following report:

==17045== 128 bytes in 1 blocks are definitely lost in loss record 6 of 11
==17045==    at 0x402A629: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==17045==    by 0x40AAB63: my_thread_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045==    by 0x40AAE43: my_thread_global_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045==    by 0x40A92D7: my_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045==    by 0x40863FA: mysql_server_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045==    by 0x4087B28: mysql_init (in /usr/lib/i386-linux-gnu/libmysqlclient.so.18.0.0)
==17045==    by 0x8049890: write_db(std::string, std::string, std::string) (Listener.cpp:76)
==17045==    by 0x804A692: SocketListener(void*) (Listener.cpp:182)
==17045==    by 0x4052D4B: start_thread (pthread_create.c:308)
==17045==    by 0x4582D3D: clone (clone.S:130)

The function write_db is this:

void write_db(std::string userid,std::string zona,std::string eveniment)
{
    try
    {
    MYSQL * connect;
    connect = mysql_init(NULL);
    connect = mysql_real_connect(connect,"127.0.0.1","myusr","mypwd","mytbl",0,NULL,0);
    std::string stmt = "INSERT INTO t_evenimente(placaid,codev,zona,cand) VALUES(\"" + userid + "\"," + eveniment + ",\"" + zona + "\",NOW())";
    std::cout << stmt << std::endl;
    mysql_query(connect,stmt.c_str());
    mysql_close(connect);
    std::cout << "Inserat eveniment obiectiv " << userid << std::endl;
    }
    catch (...)
    {
        std::cout <<"Exceptie MYSQL" << std::endl;
    }
}

Where is the memory leak ? I am using mysql_init and close as the documentation says…Could it be a false positive ?

  • 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-06-18T08:08:22+00:00Added an answer on June 18, 2026 at 8:08 am

    Since you’re using connect only inside your function, it’s easier not to allocate it dynamically and risk a memory leak (most likely the one you’re seeing). Also, the MySQL API is a C API and does not throw any exceptions for you to catch, which will simplify what you have now down to something like;

    void write_db(std::string userid,std::string zona,std::string eveniment)
    {
        MYSQL connect;
        mysql_init(&connect);
        mysql_real_connect(&connect,"127.0.0.1","myusr","mypwd","mytbl",0,NULL,0);
        std::string stmt = "INSERT INTO t_evenimente(placaid,codev,zona,cand) VALUES(\"" + userid + "\"," + eveniment + ",\"" + zona + "\",NOW())";
        std::cout << stmt << std::endl;
        mysql_query(&connect,stmt.c_str());
        mysql_close(&connect);
        std::cout << "Inserat eveniment obiectiv " << userid << std::endl;
    }
    

    Of course, that still leaves out error handling, which you’ll need to add back.

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

Sidebar

Related Questions

I'm writing code in python for some sort of daemon that has to execute
Im writing a daemon in C that posts data to PostgreSQL database using libpq
I'm writing a simple little file-syncing daemon in python to implement a synchronised file
I am writing a daemon that utilizes inotify to monitor file access and it
I am writing a daemon in PHP. I did not take a OS class
I will be writing my first daemon in php and I have a couple
Why is it necessary to close standard input/output/error when writing a (unix) daemon?
I'm writing myself a small server daemon in C, and the basic parts like
I'm writing a high-loaded daemon that should be run on the FreeBSD 8.0 and
I'm writing an application that runs as a system daemon (root session) for Mac

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.