访问 QApplication::arguments 时出现段错误
Segfault when accessing QApplication::arguments
在为 gui 编写包含 qt
的程序时,我偶然发现了一个问题,可以用下面的最少代码重现:
#include <iostream>
#include <QApplication>
#include <QSettings>
using namespace std;
int main(int argc, char ** argv) {
QApplication *app;
{
int tmp_argc = argc;
app = new QApplication(tmp_argc, argv);
}
QSettings settings("testvendor");
cout<<"Num of arguments: "<<app->arguments().count()<<std::endl;
return 0;
}
运行 导致核心转储(在对 QApplication::arguments 的调用中)或 Num of arguments: 0
,这显然是错误的。
如果我用 app = new QApplication(argc, argv)
实例化 app
(使用带有参数计数的无作用域变量)或删除 settings
的 declaration/definition - 程序输出 Num of arguments: 1
符合预期(这些更改中的任何一项都足够)。
我在Ubuntu
上使用Qt 5.5
,项目是cmake
为主,CMakeLists.txt
的内容:
cmake_minimum_required(VERSION 3.3)
project(qtest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_PREFIX_PATH /opt/Qt/6.5.1/gcc_64/)
find_package(Qt5Widgets REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(qtest ${SOURCE_FILES})
target_link_libraries(qtest Qt5::Widgets)
Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string."
From QT 5 Documentation。强调我的。
tmp_argc
超出范围。
QApplication *app;
{
int tmp_argc = argc;
app = new QApplication(tmp_argc, argv);
} <-- BOOM!
根据 QCoreApplication
构造函数文档中给出的要求,这是修复它的方法:
// https://github.com/KubaO/Whosebugn/tree/master/questions/app-args-35566459
#include <QtCore>
#include <memory>
std::unique_ptr<QCoreApplication> newApp(int & argc, char ** argv) {
return std::unique_ptr<QCoreApplication>(new QCoreApplication{argc, argv});
}
int main(int argc, char ** argv) {
std::unique_ptr<QCoreApplication> app(newApp(argc, argv));
QSettings settings{"testvendor"};
qDebug() << "Num of arguments: " << app->arguments().count();
}
在为 gui 编写包含 qt
的程序时,我偶然发现了一个问题,可以用下面的最少代码重现:
#include <iostream>
#include <QApplication>
#include <QSettings>
using namespace std;
int main(int argc, char ** argv) {
QApplication *app;
{
int tmp_argc = argc;
app = new QApplication(tmp_argc, argv);
}
QSettings settings("testvendor");
cout<<"Num of arguments: "<<app->arguments().count()<<std::endl;
return 0;
}
运行 导致核心转储(在对 QApplication::arguments 的调用中)或 Num of arguments: 0
,这显然是错误的。
如果我用 app = new QApplication(argc, argv)
实例化 app
(使用带有参数计数的无作用域变量)或删除 settings
的 declaration/definition - 程序输出 Num of arguments: 1
符合预期(这些更改中的任何一项都足够)。
我在Ubuntu
上使用Qt 5.5
,项目是cmake
为主,CMakeLists.txt
的内容:
cmake_minimum_required(VERSION 3.3)
project(qtest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_PREFIX_PATH /opt/Qt/6.5.1/gcc_64/)
find_package(Qt5Widgets REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(qtest ${SOURCE_FILES})
target_link_libraries(qtest Qt5::Widgets)
Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string."
From QT 5 Documentation。强调我的。
tmp_argc
超出范围。
QApplication *app;
{
int tmp_argc = argc;
app = new QApplication(tmp_argc, argv);
} <-- BOOM!
根据 QCoreApplication
构造函数文档中给出的要求,这是修复它的方法:
// https://github.com/KubaO/Whosebugn/tree/master/questions/app-args-35566459
#include <QtCore>
#include <memory>
std::unique_ptr<QCoreApplication> newApp(int & argc, char ** argv) {
return std::unique_ptr<QCoreApplication>(new QCoreApplication{argc, argv});
}
int main(int argc, char ** argv) {
std::unique_ptr<QCoreApplication> app(newApp(argc, argv));
QSettings settings{"testvendor"};
qDebug() << "Num of arguments: " << app->arguments().count();
}