Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. bad_alloc problem in QML in QT
QtWS25 Last Chance

bad_alloc problem in QML in QT

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 2 Posters 660 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.
  • J Offline
    J Offline
    Jimmy01901
    wrote on last edited by
    #1

    Hello guys,
    I am beginner in qml in qt and I have a problem. So, I'm making (or trying to make) a poweranalizer in qt qml. I just wanted to add that it's QT 5. The first problem was with Charts. But someone adviced me to change QGuiApplication to QApplication in main.qml. It was working for a few hours, but the next day I have this problem:

    QML debugging is enabled. Only use this in a safe environment.
    terminate called after throwing an instance of 'std::bad_alloc'
      what():  std::bad_alloc
    QMutex: destroying locked mutex
    

    I was looking for a solution with that problem, but with no results.
    Could you help me?

    Here's my main.cpp code:

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QTimer>
    #include <QtCharts/qchartview.h>
    
    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
    
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
            &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    My main.qml code:

    import QtQuick 2.9
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.5
    import QtQuick.Controls 1.4 as C1
    import QtCharts 2.3
    
    
    
    
    //załączone pozostałe pliki
    import "."
    
    
    
    Window {
        width: 800
        height: 480
        visible: true
        title: qsTr("Power Analizer")
    
    
        C1.TabView {
            id: tabview
            anchors.fill: parent
    
            C1.Tab{
                id: tab1
                title: "Moc"
    
                Rectangle{
                    width: tab1.width
                    height: tab1.height
                    Image {
                        source: "zdj/tlo.jpg"
                        anchors.fill: parent
                    }
    
                    //lewa strona
                    Rectangle {
                        color: "transparent"
                        anchors {
                            left: parent.left
                            top: parent.top
                        }
                        width: tab1.width /3
                        height: tab1.height
                        Image {
    
                            x: -34
                            y: 50
                            source: "zdj/amper.png"
                            width: 350
                            height: 350
                            Image{
                                id: wskazowka1_1
                                source: "zdj/needle-61.png"
                                width: 335
                                height: 335
                                anchors.centerIn: parent
                                rotation: suwak1_1.value
                            }
                        }
    
                        //suwak
                        Slider {
                            id: suwak1_1
                            width: 200
                            height: 50
                            from: 0
                            to: 120
                            stepSize: 0.1
                            anchors {
                                left: parent.left
                                leftMargin: 40
                                bottom: parent.bottom
                                bottomMargin: 30
                            }
    
                        }
    
                    }
    
    
                    //prawa strona
                    Rectangle {
                        color: "transparent"
                        anchors {
                            right: parent.right
                            top: parent.top
                        }
                        width: tab1.width /3
                        height: tab1.height
                        Image {
                            x: -50
                            y: 50
                            source: "zdj/volt.png"
                            width: 350
                            height: 350
                            Image{
                                id: wskazowka1_3
                                source: "zdj/needle-63.png"
                                width: 350
                                height: 350
                                anchors.centerIn: parent
                                rotation: suwak1_3.value
                            }
                        }
    
                        //przycisk zamknięcia
                        Close {
                            text: "Zamknij"
                            onClicked: {
                            close()
                            }
                        }
    
                        //suwak
                        Slider {
                            id: suwak1_3
                            width: 200
                            height: 50
                            from: 0
                            to: 124
                            stepSize: 0.1
                            anchors {
                                left: parent.left
                                leftMargin: 40
                                bottom: parent.bottom
                                bottomMargin: 30
                            }
    
                        }
    
                    }
    
    
                    //środek
                    Rectangle {
                        color: "transparent"
                        anchors {
                            centerIn: parent
                        }
                        width: tab1.width /3
                        height: tab1.height
                        Image {
                            anchors.centerIn: parent
                            source: "zdj/kWatt.png"
                            width: 550
                            height: 550
                            Image{
                                id: wskazowka1_2
                                source: "zdj/needle-63.png"
                                width: 550
                                height: 550
                                anchors.centerIn: parent
                                rotation: (suwak1_3.value === 0 || suwak1_1.value === 0) ? 0 : suwak1_1.value * suwak1_3.value/120
                            }
                        }
                    }
                }
            }
    
            C1.Tab{
                id: tab2
                title: "Tab_2"
    
                ChartView {
                    id: chart
                    anchors.fill: parent
                    theme: ChartView.ChartThemeBrownSand
                    antialiasing: true
    
                    SplineSeries {
                        name: "sin(x)"
                        XYPoint { x: 0; y: 1 }
                        XYPoint { x: 2; y: -1 }
                        XYPoint { x: 4; y: 1 }
                        XYPoint { x: 6; y: -1 }
                        XYPoint { x: 8; y: 1 }
                        XYPoint { x: 10; y: -1 }
                        XYPoint { x: 12; y: 1 }
                    }
                }
            }
    
            C1.Tab{
                id: tab3
                title: "Tab_3"
                Rectangle{
                    width: tab3.width
                    height: tab3.height
                    color: "green"
                    Close {
                        text: "Zamknij"
                        onClicked: {
                        close()
                        }
                    }
                }
    
    
            }
        }
    
    }
    

    And my poweranalizer.pro code:

    QT += quick \
        widgets
    QT += charts
    
    CONFIG += c++11
    CONFIG += qml_debug
    
    # You can make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp
    
    RESOURCES += qml.qrc
    
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH =
    
    # Additional import path used to resolve QML modules just for Qt Quick Designer
    QML_DESIGNER_IMPORT_PATH =
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    DISTFILES +=
    
    HEADERS +=
    
    

    and the Close.qml file code:

    import QtQuick 2.0
    import QtGraphicalEffects 1.0
    import QtQuick.Controls 2.5
    
    
    Button {
        id: zamknij
        anchors{
            right: parent.right
            top: parent.top
            rightMargin: 20
            topMargin: 20
        }
        text: qsTr("Zamknij")
        property color backgroundDefaultColor: "#E56043"
        property color backgroundPressedColor: Qt.darker(backgroundDefaultColor, 1.5)
    
        background: Rectangle {
            implicitWidth: 75
            implicitHeight: 25
            color: zamknij.down ? zamknij.backgroundPressedColor : zamknij.backgroundDefaultColor
            radius: 5
        }
    }
    

    P.S. Sorry for my English, its not my primary language :)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Which exact version of Qt are you using ?
      On which OS ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Which exact version of Qt are you using ?
        On which OS ?

        J Offline
        J Offline
        Jimmy01901
        wrote on last edited by
        #3

        @SGaist it’s 5.2 QT version on Windows

        J 1 Reply Last reply
        0
        • J Jimmy01901

          @SGaist it’s 5.2 QT version on Windows

          J Offline
          J Offline
          Jimmy01901
          wrote on last edited by
          #4

          Or it’s QT 5.12.12 in files

          SGaistS 1 Reply Last reply
          0
          • J Jimmy01901

            Or it’s QT 5.12.12 in files

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Jimmy01901 Can you clarify that ?

            In any case, you should update to a more recent version of Qt. Either the 5.15 series or even better, Qt 6.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            J 2 Replies Last reply
            0
            • SGaistS SGaist

              @Jimmy01901 Can you clarify that ?

              In any case, you should update to a more recent version of Qt. Either the 5.15 series or even better, Qt 6.

              J Offline
              J Offline
              Jimmy01901
              wrote on last edited by
              #6

              @SGaist sorry, it’s 5.15 😅

              1 Reply Last reply
              0
              • SGaistS SGaist

                @Jimmy01901 Can you clarify that ?

                In any case, you should update to a more recent version of Qt. Either the 5.15 series or even better, Qt 6.

                J Offline
                J Offline
                Jimmy01901
                wrote on last edited by
                #7

                @SGaist I prefer to learn on 5 series, maybe in the future I will switch to qt 6, but for now I feel much better in the previous one. I have an update about my problem, 5 minutes ago everything was fine. Program has properly started, but next compilation wasn't as good. It displays bad alloc problem and other weird problems. I'm attaching photos of 3 consecutive results.

                1 compilation
                1.png

                2 compilation
                2.png

                3 compilation
                3.png

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  This looks really strange...

                  What are your computer specifications ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    This looks really strange...

                    What are your computer specifications ?

                    J Offline
                    J Offline
                    Jimmy01901
                    wrote on last edited by
                    #9

                    @SGaist cpu: Intel Core i5-12400F gpu: Asus Radeon RX6750 XT and 64 GB ram

                    J 1 Reply Last reply
                    0
                    • J Jimmy01901

                      @SGaist cpu: Intel Core i5-12400F gpu: Asus Radeon RX6750 XT and 64 GB ram

                      J Offline
                      J Offline
                      Jimmy01901
                      wrote on last edited by
                      #10

                      @Jimmy01901 For now I added a code to the push button which I called “Close”:

                      destory(Application)
                      close() // it was before 
                      Application.destroyed : console.log (“memory clear”) : console.log (“memory full”)
                      

                      And every time when I’m compiling everything seems to be good, but the console.log displays “memory full”

                      1 Reply Last reply
                      0

                      • Login

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