c++qtc++11qt5qcustomplot

Qt5 throws std::bad_alloc


I'm trying to use QCustomPlot in my console application. I started by creating a simple wrapper for it that's suitable for my uses. The wrapper was supposed to However, I get std::bad_alloc errors every time I try to show the window.

This is my code, I've created a wrapper class in Plot.hpp:

class Plot
{
    private:
        std::string name;
        QApplication* app;
        QMainWindow* window;
        QCustomPlot* plotWidget;
    public:
        Plot(std::string& name);
        // OTHER METHODS
        void showPlot();
};

and in my Plot.cpp file I have the following:

Plot::Plot(std::string& name) : name(name)
{
    char *gui_argv[] = {(char*)(name.c_str()), NULL};
    int gui_argc = sizeof(gui_argv) / sizeof(char*) - 1;
    app = new QApplication(gui_argc, gui_argv);
    window = new QMainWindow();
    // Add plot Widget
    plotWidget = new QCustomPlot(window);
    window->setCentralWidget(plotWidget);
    plotWidget->plotLayout()->clear();
}

// OTHER METHODS

void Plot::showPlot()
{
    // Run the GUI
    window->show();
    app->exec();
}

and I have the following in my main.cpp:

int main()
{
    std::string title = "Testing";
    Util::Plot *plotWindow = new Util::Plot(title);
    // NO OTHER STATEMENTS
    plotWindow->showPlot();
    return 0;
}

Through GDB I got this stack trace but I couldn't really decipher it to find what the error is. It goes deep into the internals of QT:

#0  0x00007ffff7279e97 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007ffff727b801 in __GI_abort () at abort.c:79
#2  0x00007ffff78d08fb in  () at /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff78d6d3a in  () at /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff78d6d95 in  () at /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff78d6fe8 in  () at /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff529e402 in  () at /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#7  0x00007ffff530a22a in QListData::detach(int) () at /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#8  0x00007ffff534475e in  () at /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#9  0x00007ffff549a48f in QCoreApplication::arguments() () at /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#10 0x00007fffef9e3791 in  () at /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5
#11 0x00007fffef9e3c8d in QXcbIntegration::wmClass() const () at /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5
#12 0x00007fffef9f8e03 in QXcbWindow::create() () at /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5
#13 0x00007fffef9e4bfb in QXcbIntegration::createPlatformWindow(QWindow*) const () at /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so.5
#14 0x00007ffff5a6229e in QWindowPrivate::create(bool, unsigned long long) () at /usr/lib/x86_64-linux-gnu/libQt5Gui.so.5
#15 0x00007ffff6245add in QWidgetPrivate::create_sys(unsigned long long, bool, bool) () at /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#16 0x00007ffff624619d in QWidget::create(unsigned long long, bool, bool) () at /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
#17 0x00007ffff6252a96 in QWidget::setVisible(bool) () at /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5
# The following is line window->show()
#18 0x00007ffff7bd3179 in Util::Plot::showPlot() (this=0x55555576fd80) at ./lib/util/Plot.cpp:71
#19 0x00005555555549b3 in main() () at ./lib/test/PlotTest.cpp:16

I've also verified that the pointers to window, app, and plotWidget are not null. So basically, just creating the QMainWindow and trying to show it without even doing any other actions causes this failure to occur. What could be wrong here? What am I missing?

Extra: I don't think the following is the cause of the issue. But just in case: I'm not using QT Studio, I have written my own makefile that builds libQCustomPlot.so and my own app and links them against the necessary QT libraries. No failures or warnings through compilation.

Edit1: I forgot to post the original error! It's just the following with no additional info/clarification:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

Solution

  • QApplication takes argc by reference and expects this reference to be valid for the lifetime of the application. Once your Plot function ends, QApplication is left with a dangling reference to gui_argc so when the call to QApplication::arguments (as seen in your back trace) is made, undefined behavior occurs. You can fix this by making argc persistent somewhere.