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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:24:09+00:00 2026-06-01T00:24:09+00:00

I am allocating memory for an object and if a particular statement following the

  • 0

I am allocating memory for an object and if a particular statement following the memory allocation fails, I have to delete the memory and also throw an exception.

For example say

               QSqlQuery *query =  new QSqlQuery(db);
               try {
                query->prepare(somestmt);
               }
               catch (...) {
                throwException(*query);
               }

Here where and how should I delete query if an exception is thrown?

Thanks!

  • 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-01T00:24:10+00:00Added an answer on June 1, 2026 at 12:24 am

    The answer depends on how long you need the query object to stay alive. If you don’t need it outside the try/except block, then it’s probably best to use RAII to delete it as soon as you leave that block. E.g., using boost::scoped_ptr, you could do this:

    try {
        boost::scoped_ptr<QSqlQuery> query(new QSqlQuery(db));
        query->prepare(somestmt);
    }
    catch (...) {
        throwException();
    }
    

    But from your example, it looks like you want to keep the query object, or a copy of it, around past that block. If it’s ok to just keep a copy of it, then you could do this:

    boost::scoped_ptr<QSqlQuery> query(new QSqlQuery(db));
    try {
        query->prepare(somestmt);
    }
    catch (...) {        
        QSqlQuery copyOfQuery(*query);
        throwException(copyOfQuery);
    }
    

    Or this, if you don’t like using boost:

    QSqlQuery* query = new QSqlQuery(db);
    try {
        query->prepare(somestmt);
    }
    catch (...) {        
        QSqlQuery copyOfQuery(*query);
        delete query;
        throwException(copyOfQuery);
    }
    delete query;
    

    If you need to keep the original query itself, then you probably would need to require the exception object itself to delete it. I.e., do something like this:

    QSqlQuery* query = new QSqlQuery(db);
    try {
        query->prepare(somestmt);
    }
    catch (...) {        
        throw MyException(query);
    }
    delete query;
    

    Where part of “MyException”‘s contract is that it takes ownership of its argument (i.e., responsibility for deleting it).

    Another option would be to use shared pointers. I.e.:

    boost::shared_ptr<QSqlQuery> query(new QSqlQuery(db));
    try {
        query->prepare(somestmt);
    }
    catch (...) {        
        throwException(query);
    }
    

    This has the advantage that the query gets deleted when the last shared pointer to it goes away, which makes memory management much easier.

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

Sidebar

Related Questions

I am allocating memory to a object dynamically and then if I call delete
I have a strong use case for pre-allocating all the memory I need upfront
I have a program that uses way too much memory for allocating numerous small
Which is preferable way to allocate memory for a function that is frequently allocating
I am using mmap for allocation memory and mark some pages as PROT_READ and
Looks like dynamic memory allocation without garbage collection is a way to disaster. Dangling
Here I'm gettimg memory allocation problem at activity indicator and my code is: -
I want to measure time memory allocation using this code: long AForMemory = DateTime.Now.Ticks;
I thought these two methods were (memory allocation-wise) equivalent, however, I was seeing out
I was just looking into the memory allocation of a program in C .

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.