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 9204641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:45:30+00:00 2026-06-17T23:45:30+00:00

I will create a connection to a Postgresql db through libpqxx and C++ and

  • 0

I will create a connection to a Postgresql db through libpqxx and C++ and then execute several prepared statements, which return results (and I will loop through them). I come from a Java background and the process is:

  1. Open the db connection
  2. prepare the statements
  3. adjust prepared statements params
  4. execute the statements
  5. loop through resultset
  6. close resultset
  7. close prepared statement
  8. close db connection

I have sample code for 1-5 and 8, but I cannot find how to close result object and prepared statement object

Sample code:

connection C("dbname=mydbname user=postgres password=mypass hostaddr=127.0.0.1 port=5432");
string tableName("mydbtable");
if (C.is_open()) {
    cout << "We are connected to " << C.dbname() << endl;
} else {
    cout << "We are not connected!" << endl;
}

result r;
try {
    const std::string sql =
            "SELECT * FROM " + tableName + " WHERE sn_autoinc10 = $1";
    C.prepare("find", sql);
    //C.prepare("findtable", ) ("integer");
    work W(C);
    r = W.prepared("find")(0).exec();
    for (int rownum = 0; rownum < r.size(); ++rownum) {
        const result::tuple row = r[rownum];
        for (int colnum = 0; colnum < row.size(); ++colnum) {
            const result::field myField = row[colnum];
            std::cout << myField.c_str() << ' ';
        }
        std::cout << std::endl;
    }
    C.disconnect();
} catch (const std::exception &e) {
    std::cerr << e.what() << std::endl;
}

Do I need to explicitly close result and prepared statement with c++ and libpqxx in order to avoid memory leaks? Thanks in advance

  • 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-17T23:45:32+00:00Added an answer on June 17, 2026 at 11:45 pm

    Transaction and result objects are automatically cleaned when they’re deleted, which means that they’re cleaned when they go out of scope.

    So your code won’t leak memory, but it isn’t perfect:

    • you should not reuse r variable – it is better to declare result object inside a try block, so it would be cleared as soon as not needed;
    • you should not call C.disconnect() inside try block – it’s better to just allow C to go out of scope.

    In C++ you’re not supposed to declare variables outside of minimal needed scope – let compiler optimize this for you.

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

Sidebar

Related Questions

According to PHP.net manual, pg_pconnect will create a persistent connection, or will return the
The target is: I need a wcf method which will create user in the
I have written a program that use one single connection with prepared statements. So
i want to use my ldap server as a DB. then i will create
Code bellow will create eventstore for named connection. var es = Wireup.Init() .UsingSqlPersistence(DB) .Build();
I want to create a factory which will create a smack XMPPConnection. The factory
I have created a class that will manage connection and commands, I named it
I will create 5 tables, namely data1, data2, data3, data4 and data5 tables. Each
I will create multiple sale versions of the same application with push notifications feature:
I will create a webpage that has a sidebar, and the sidebar will have

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.