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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:17:42+00:00 2026-06-07T12:17:42+00:00

I have a class where I allocate some memory. For Example: class image {

  • 0

I have a class where I allocate some memory. For Example:

class image {

public:
    image(){
        pixelvalue = 0;
    }

    void formatandcopy() {
        pixelvalue = new int [10000*50000];
        if(pixelvalue)
            qDebug()<<"allocation successful"; 
        else 
            qDebug()<<"allocation failed";
    }
private:
    int *pixelvalue;
};

When I call formatandcopy() the program throws this:

Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there.

Anyone know how I can prevent this and let the user know it’s simply out of memory? When I run this it doesn’t even show allocation failed. The above error is thrown before the qDebug() is called. The program runs fine if the amount of memory allocated is decreased. I thought this was strange since this error is thrown when using the new operator and not a qt function. Furthermore my machine has plenty of memory left. I assume this is a result of qt limiting it’s program to a certain heap space. Lastly, if I can fix this by indeed reimplementing the notify function then can anyone point me in the right direction to do this?

  • 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-07T12:17:45+00:00Added an answer on June 7, 2026 at 12:17 pm

    You should be able to catch std::bad_alloc to handle the exception within that function. The exception is part of standard C++.

    try {
      // ...
    } catch (std::bad_alloc &a) {
      // ...
    }
    

    If it gets beyond that scope (into the Qt event handling) then you will have to implement QApplication::notify as they specify.

    As a word of warning, bad allocations usually are not recoverable. The exception is when you know you are allocating a very large amount (perhaps based on user input) when you typically will not be using a very memory hungry application.

    EDIT:

    To clarify, if the design of your application allows you to run out of memory, then it is unlikely that anything will change if you catch bad_allocs and ignore them. The program is dead, you are only going to be able to display an error message about what happened. That is also tricky, because you cannot allocate any memory to create the message box!

    The counter example is a scenario like asking the user for a file and reading it all into memory. Eventually they will try and give you a file that they don’t have the memory for and you can safely tell them to try another file. These sorts of problems are usually isolated in an application and well worth guarding against.

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

Sidebar

Related Questions

I have a class constructor like this: public JavoImageCorrectedDataHeader() { ByteBuffer buffer = ByteBuffer.allocate(this.size());
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
We have the concept of pointers in C++. Now if we allocate some memory
I'm trying to design a class that needs to dynamically allocate some memory.. I
I have a class that is using boost::aligned storage to statically allocate memory within
I am new to C++ but I have some basic memory allocation knowledge in
I need some design suggestions for an Image class hierarchy. Currently, I have 2
I have class with back reference: public class Employee : Entity { private string
Lets say, for example, that I have a class that requires the use of
I have a fictional class: template<typename T> class demonstration { public: demonstration(){} ... T

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.