[英]Qt5 throws std::bad_alloc
我正在嘗試在控制台應用程序中使用QCustomPlot。 我首先為它創建了一個適合自己使用的簡單包裝器。 但是,每次嘗試顯示窗口時,我都會收到std :: bad_alloc錯誤。
這是我的代碼,我在Plot.hpp
創建了一個包裝器類:
class Plot
{
private:
std::string name;
QApplication* app;
QMainWindow* window;
QCustomPlot* plotWidget;
public:
Plot(std::string& name);
// OTHER METHODS
void showPlot();
};
在我的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();
}
我的main.cpp
有以下內容:
int main()
{
std::string title = "Testing";
Util::Plot *plotWindow = new Util::Plot(title);
// NO OTHER STATEMENTS
plotWindow->showPlot();
return 0;
}
通過GDB,我獲得了該堆棧跟蹤,但是我無法真正對其進行解密以找到錯誤所在。 它深入到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
我還驗證了指向window
, app
和plotWidget
的指針不為null。 因此,基本上,僅創建QMainWindow
並嘗試顯示它而不執行任何其他操作就可能導致此失敗。 這有什么問題嗎? 我想念什么?
另外:我不認為是造成此問題的原因。 但以防萬一:我沒有使用QT Studio,而是編寫了自己的makefile,該文件生成libQCustomPlot.so
和我自己的應用程序,並將它們鏈接到必要的QT庫。 通過編譯沒有失敗或警告。
Edit1:我忘記發布原始錯誤! 只是以下內容,沒有其他信息/說明:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
QApplication通過引用獲取argc
,並希望此引用在應用程序的生命周期內有效。 一旦您的Plot函數結束,QApplication就會留下對gui_argc
的懸空引用,因此,當對QApplication :: arguments的調用(如您在追溯中所看到的)時,將發生未定義的行為。 您可以通過將argc
持久化到某個位置來解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.