Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Retrieving the object info due to exception std::bad_alloc in Qt 4.8
QtWS25 Last Chance

Retrieving the object info due to exception std::bad_alloc in Qt 4.8

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
12 Posts 4 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ramakanth
    wrote on 27 Feb 2018, 14:03 last edited by
    #1

    Hi,

    Am using open source version of Qt 4.8.6 sdk.
    As mentioned in http://doc.qt.io/archives/qt-4.8/exceptionsafety.html, the below code is being used to catch the exceptions in application.

    QApplication app(argc, argv);
    ...
    try {
    app.exec();
    } catch (const std::bad_alloc &) {
    //clean up code and log the exception info
    // retrieve class name and method name of object
    return 0; // exit the application
    }

    Is there any means to get the runtime object info like class name, method name(if possible line number) throwing the exception std::bad_alloc in catch block?

    Please let me know

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ambershark
      wrote on 27 Feb 2018, 21:54 last edited by
      #2

      You could call what() on the bad_alloc object. That should give you a string of what happened.

      But as for finding the QObject that may have had the allocation issue I don't know of a way to do that from an std::bad_alloc exception.

      Also, I don't think I've ever caught allocation exceptions. Are you running a low memory or embedded application that has RAM issues or something? Normally you wouldn't worry about running out of memory since the kernel will handle it for you. And by the time it fails your system would be non-responsive anyway.

      And finally, if you have an error condition never return 0 from main. It indicates you successfully exited. Always return a positive integer. Even just returning 1 is fine since at least the OS knows the app failed.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      2
      • R Offline
        R Offline
        Ramakanth
        wrote on 28 Feb 2018, 05:07 last edited by
        #3

        @ambershark said in Retrieving the object info due to exception std::bad_alloc in Qt 4.8:

        Are you running a low memory or embedded application that has RAM issues or something

        Yes am running a low memory or embedded application that has RAM issues. I would like to catch all the exceptions including std::bad_alloc like below:-

        try {
        uiApp->exec();
        }
        catch (const std::exception& e)
        {
        Q_UNUSED(e);
        qDebug()<< e.what();

        }
        

        But not able to get the object runtime info like class and method name which had thrown exception.

        A J 2 Replies Last reply 28 Feb 2018, 06:09
        0
        • R Ramakanth
          28 Feb 2018, 05:07

          @ambershark said in Retrieving the object info due to exception std::bad_alloc in Qt 4.8:

          Are you running a low memory or embedded application that has RAM issues or something

          Yes am running a low memory or embedded application that has RAM issues. I would like to catch all the exceptions including std::bad_alloc like below:-

          try {
          uiApp->exec();
          }
          catch (const std::exception& e)
          {
          Q_UNUSED(e);
          qDebug()<< e.what();

          }
          

          But not able to get the object runtime info like class and method name which had thrown exception.

          A Offline
          A Offline
          ambershark
          wrote on 28 Feb 2018, 06:09 last edited by
          #4

          @Ramakanth I don't know of a way for you to get the information with an std exception.

          You could get it if you let the application crash in a debugger and did a backtrace. However while it is running and that is the only exception you get, then .what() is the best you're going to get.

          You could try creating custom new and delete operators that log the information but that really does seem like overkill.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          R 2 Replies Last reply 28 Feb 2018, 06:55
          1
          • A ambershark
            28 Feb 2018, 06:09

            @Ramakanth I don't know of a way for you to get the information with an std exception.

            You could get it if you let the application crash in a debugger and did a backtrace. However while it is running and that is the only exception you get, then .what() is the best you're going to get.

            You could try creating custom new and delete operators that log the information but that really does seem like overkill.

            R Offline
            R Offline
            Ramakanth
            wrote on 28 Feb 2018, 06:55 last edited by
            #5

            @ambershark said in Retrieving the object info due to exception std::bad_alloc in Qt 4.8:

            You could try creating custom new and delete operators that log the information but that really does seem like overkill.

            Do you mean overloading the custom new and delete operators in a custom class and inherit the custom class across the application?

            J 1 Reply Last reply 28 Feb 2018, 08:41
            0
            • R Ramakanth
              28 Feb 2018, 06:55

              @ambershark said in Retrieving the object info due to exception std::bad_alloc in Qt 4.8:

              You could try creating custom new and delete operators that log the information but that really does seem like overkill.

              Do you mean overloading the custom new and delete operators in a custom class and inherit the custom class across the application?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 28 Feb 2018, 08:41 last edited by
              #6

              @Ramakanth See http://en.cppreference.com/w/cpp/memory/new/operator_new

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • A ambershark
                28 Feb 2018, 06:09

                @Ramakanth I don't know of a way for you to get the information with an std exception.

                You could get it if you let the application crash in a debugger and did a backtrace. However while it is running and that is the only exception you get, then .what() is the best you're going to get.

                You could try creating custom new and delete operators that log the information but that really does seem like overkill.

                R Offline
                R Offline
                Ramakanth
                wrote on 28 Feb 2018, 10:15 last edited by
                #7

                You could try creating custom new and delete operators that log the information but that really does seem like overkill.
                @ambershark Any reasons why it would be overkill? Whether it is not recommended or best practice?

                A 1 Reply Last reply 28 Feb 2018, 21:40
                0
                • R Ramakanth
                  28 Feb 2018, 10:15

                  You could try creating custom new and delete operators that log the information but that really does seem like overkill.
                  @ambershark Any reasons why it would be overkill? Whether it is not recommended or best practice?

                  A Offline
                  A Offline
                  ambershark
                  wrote on 28 Feb 2018, 21:40 last edited by ambershark
                  #8

                  @Ramakanth Well because every single new/delete will have extra work to do. Which means more cpu cycles. That directly slows your application down.

                  As for the overkill part, managing memory to this level is not needed in most applications. Only if you are writing a critical system like for an airplane or a hospital or something like that would you need to worry about this level of exception.

                  Like I said before if you can't allocate memory and it throws that exception your system is already in a non-responsive state. You would be far into virtual memory and everything would be crawling. It would feel locked up.

                  I've written millions of lines of code and many commercial products, and even some fairly critical systems on embedded devices. Never once needed to worry about catching that exception. By the time your system is out of memory it's pretty much dead anyway. :)

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  3
                  • R Ramakanth
                    28 Feb 2018, 05:07

                    @ambershark said in Retrieving the object info due to exception std::bad_alloc in Qt 4.8:

                    Are you running a low memory or embedded application that has RAM issues or something

                    Yes am running a low memory or embedded application that has RAM issues. I would like to catch all the exceptions including std::bad_alloc like below:-

                    try {
                    uiApp->exec();
                    }
                    catch (const std::exception& e)
                    {
                    Q_UNUSED(e);
                    qDebug()<< e.what();

                    }
                    

                    But not able to get the object runtime info like class and method name which had thrown exception.

                    J Offline
                    J Offline
                    JonB
                    wrote on 1 Mar 2018, 09:42 last edited by
                    #9

                    @Ramakanth said in Retrieving the object info due to exception std::bad_alloc in Qt 4.8:

                    Yes am running a low memory or embedded application that has RAM issues.

                    As others have said, by the time you get down to one new falling over you're passed the point where you can recover properly. However, if this memory is an issue, what you could (maybe should) do is perform your own check for "plenty of remaining memory" intermittently or at a "safe" point of code, well before you get to no memory left at all. I don't know what the call is for that (perhaps native, not a Qt one), but I imagine there should be some way of detecting that you're getting "low" on remaining VM and deal with it then. Not perfect/scientific, but might be what you need.

                    1 Reply Last reply
                    3
                    • A Offline
                      A Offline
                      ambershark
                      wrote on 2 Mar 2018, 05:29 last edited by
                      #10

                      You could even take it a step further and do your own memory pool. That way you have a preallocated block reserved for your application and when/if it runs out it isn't when the system is dying from lack of RAM.

                      I don't know what you're working on, but this is usually overkill for 99% of apps out there.

                      I've done it before, just to learn back in my beginning c++ days. That was back in the 16 bit world where memory really was an issue. Your common PC back then had 64MB RAM if you were cool. ;)

                      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                      J 1 Reply Last reply 2 Mar 2018, 07:27
                      1
                      • A ambershark
                        2 Mar 2018, 05:29

                        You could even take it a step further and do your own memory pool. That way you have a preallocated block reserved for your application and when/if it runs out it isn't when the system is dying from lack of RAM.

                        I don't know what you're working on, but this is usually overkill for 99% of apps out there.

                        I've done it before, just to learn back in my beginning c++ days. That was back in the 16 bit world where memory really was an issue. Your common PC back then had 64MB RAM if you were cool. ;)

                        J Offline
                        J Offline
                        JonB
                        wrote on 2 Mar 2018, 07:27 last edited by JonB 3 Feb 2018, 07:33
                        #11

                        @ambershark

                        I've done it before, just to learn back in my beginning c++ days. That was back in the 16 bit world where memory really was an issue. Your common PC back then had 64MB RAM if you were cool. ;)

                        64MB? Don't you mean 640K?? Actually that might have been C days, not C++.

                        Stop showing off how young you are ;-) In my beginning days, that was back in 8-bit world, where memory was really, really an issue. Your common Sinclair ZX-81 then had 1K RAM, though you could expand that to 16K (dongle on the back, but it tended to fall out as it was too heavy for the slot) if you were cool... ;)

                        A 1 Reply Last reply 2 Mar 2018, 20:42
                        0
                        • J JonB
                          2 Mar 2018, 07:27

                          @ambershark

                          I've done it before, just to learn back in my beginning c++ days. That was back in the 16 bit world where memory really was an issue. Your common PC back then had 64MB RAM if you were cool. ;)

                          64MB? Don't you mean 640K?? Actually that might have been C days, not C++.

                          Stop showing off how young you are ;-) In my beginning days, that was back in 8-bit world, where memory was really, really an issue. Your common Sinclair ZX-81 then had 1K RAM, though you could expand that to 16K (dongle on the back, but it tended to fall out as it was too heavy for the slot) if you were cool... ;)

                          A Offline
                          A Offline
                          ambershark
                          wrote on 2 Mar 2018, 20:42 last edited by ambershark 3 Feb 2018, 20:43
                          #12

                          @JonB Lol ... my very first computer was an Apple ][+ with 48K of RAM. That was when I was about 8. It was definitely 8 bit back then. But I didn't learn C++ until late high school/early college so yea 64MB was all sorts of cool at that time.

                          Although I knew about the sinclairs I never actually saw one since they were "outdated" by the apples.

                          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                          1 Reply Last reply
                          0

                          2/12

                          27 Feb 2018, 21:54

                          10 unread
                          • Login

                          • Login or register to search.
                          2 out of 12
                          • First post
                            2/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved