简体   繁体   English

Qt5抛出std :: bad_alloc

[英]Qt5 throws std::bad_alloc

I'm trying to use QCustomPlot in my console application. 我正在尝试在控制台应用程序中使用QCustomPlot。 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. 但是,每次尝试显示窗口时,我都会收到std :: bad_alloc错误。

This is my code, I've created a wrapper class in Plot.hpp : 这是我的代码,我在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.cpp文件中有以下内容:

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 : 我的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. 通过GDB,我获得了该堆栈跟踪,但是我无法真正对其进行解密以找到错误所在。 It goes deep into the internals of QT: 它深入到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. 我还验证了指向windowappplotWidget的指针不为null。 So basically, just creating the QMainWindow and trying to show it without even doing any other actions causes this failure to occur. 因此,基本上,仅创建QMainWindow并尝试显示它而不执行任何其他操作就可能导致此失败。 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. 但以防万一:我没有使用QT Studio,而是编写了自己的makefile,该文件生成libQCustomPlot.so和我自己的应用程序,并将它们链接到必要的QT库。 No failures or warnings through compilation. 通过编译没有失败或警告。

Edit1: I forgot to post the original error! Edit1:我忘记发布原始错误! 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)

QApplication takes argc by reference and expects this reference to be valid for the lifetime of the application. QApplication通过引用获取argc ,并希望此引用在应用程序的生命周期内有效。 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. 一旦您的Plot函数结束,QApplication就会留下对gui_argc的悬空引用,因此,当对QApplication :: arguments的调用(如您在追溯中所看到的)时,将发生未定义的行为。 You can fix this by making argc persistent somewhere. 您可以通过将argc持久化到某个位置来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM