- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从 GET_STATION_MODEL_PARAMS_t 访问 QString 类型的结构成员时,我看到了一个奇怪的段错误。执行在 **
处停止:
代码:
#include <QDebug>
#include "hradcontroller.h"
#include "hradAPI.h"
#define TAG "HRAD_CONTROLLER:\0"
#define HRAD_API_VERSION "2"
#define MANUFACTURER_KEY "abcde12345"
typedef struct registerDeviceParams{
QString brand;
QString device;
QString manufacturer;
QString model;
QString serial;
QString ibd;
QString ibkey;
QString version;
QString track;
registerDeviceCB_t cbFn;
}REGISTER_DEVICE_PARAMS_t;
typedef struct getStationModelParams{
int stationID;
QString ibd;
QString ibkey;
QString version;
QString track;
getStationModelCB_t cbFn;
GET_STATION_MODEL_RESPONSE_t* resp;
}GET_STATION_MODEL_PARAMS_t;
HRAD *hbr = new HRAD();
void registerDevice(){
REGISTER_DEVICE_PARAMS_t params;
params.brand = "brand";
params.device = "TI-J6";
params.manufacturer = "manuf";
params.model = "EVK";
params.ibkey = MANUFACTURER_KEY;
params.serial = "abcde12345";
params.version = HRAD_API_VERSION;
params.track = "true";
params.cbFn = deviceRegisteredCB;
hbr->registerDevice(¶ms);
}
void getStationModel(int id){
GET_STATION_MODEL_PARAMS_t params;
params.stationID = id;
params.ibd = hbr->getId();
params.ibkey = "abdef";
params.version = "2";
** params.track = "false"; //SIGSEGV segmantation fault here
params.cbFn = stationModelAvailableCB;
params.resp = &g_StationModel;
}
void deviceRegisteredCB(QString ibd){
qDebug() << TAG << "Device Registered: ibd = " << ibd;
getStationModel(currentPublicStationID);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* Register ourselves to HRAD Server */
registerDevice();
}
段之前的值。故障:
如果您在调试时注意到“track”“不可访问”,这会导致崩溃。
我这样修改了 GET_STATION_MODEL_PARAMS_t:
typedef struct getStationModelParams{
int stationID;
QString foo; //dummy variable.
QString ibd;
QString ibkey;
QString version; //seg. fault here now
QString track;
getStationModelCB_t cbFn;
GET_STATION_MODEL_RESPONSE_t* resp;
}GET_STATION_MODEL_PARAMS_t;
值(value)观变成了这样:
现在“版本”也“不可访问”。
知道为什么会这样吗?
谢谢。
更新 1:
工具:
Qt Creator 3.4.1 (ubuntu 14.04)
Qt 5.4.2(GCC 4.6.1,64 位)
最佳答案
TL;DR:这就是为什么您真的需要发布重现崩溃所需的所有代码。
您的代码有效 - 没有崩溃。你的问题就这样得到了回答,即使这个答案对你和其他人都毫无用处。 不要问只能以无法帮助您的方式回答的问题。
除了它是一个在现代 C++ 中没有立足之地的可怕的 C 可憎之外,在您的代码中唯一突出的是 HRAD 实例是在 main
开始执行之前创建的。根据 HRAD 的构造函数正在执行的操作,这可能是一件坏事。
但让我们看看我们是否可以修复您的代码,以指导您编写更安全的代码。
首先,让我们逐字逐句地开始您的代码,在开头和结尾添加一些内容以使其能够编译和执行。
你应该在你的问题中提供所有这些 - 有足够的“其他东西”崩溃。下面,假设它崩溃或以其他方式重现你的问题,将被视为 sscce并使您的问题对社区有值(value)。
#include <QString>
#include <QDebug>
typedef void (*registerDeviceCB_t)(QString);
typedef void (*getStationModelCB_t)();
typedef struct getStationModelResponse {} GET_STATION_MODEL_RESPONSE_t;
void deviceRegisteredCB(QString);
void stationModelAvailableCB() {}
typedef struct registerDeviceParams REGISTER_DEVICE_PARAMS_t;
struct HRAD {
void registerDevice(REGISTER_DEVICE_PARAMS_t*);
QString getId() { return QString(); }
};
GET_STATION_MODEL_RESPONSE_t g_StationModel;
int currentPublicStationID = 1;
void registerDevice();
void getStationModel(int);
int main()
{
registerDevice();
getStationModel(0);
return 0;
}
// Your code verbatim beyond this point
您的代码,复制粘贴以供引用(显然不相关的 MainWindow
已删除):
#define TAG "HRAD_CONTROLLER:\0"
#define HRAD_API_VERSION "2"
#define MANUFACTURER_KEY "abcde12345"
typedef struct registerDeviceParams{
QString brand;
QString device;
QString manufacturer;
QString model;
QString serial;
QString ibd;
QString ibkey;
QString version;
QString track;
registerDeviceCB_t cbFn;
}REGISTER_DEVICE_PARAMS_t;
typedef struct getStationModelParams{
int stationID;
QString ibd;
QString ibkey;
QString version;
QString track;
getStationModelCB_t cbFn;
GET_STATION_MODEL_RESPONSE_t* resp;
}GET_STATION_MODEL_PARAMS_t;
HRAD *hbr = new HRAD();
void registerDevice(){
REGISTER_DEVICE_PARAMS_t params;
params.brand = "brand";
params.device = "TI-J6";
params.manufacturer = "manuf";
params.model = "EVK";
params.ibkey = MANUFACTURER_KEY;
params.serial = "abcde12345";
params.version = HRAD_API_VERSION;
params.track = "true";
params.cbFn = deviceRegisteredCB;
hbr->registerDevice(¶ms);
}
void getStationModel(int id){
GET_STATION_MODEL_PARAMS_t params;
params.stationID = id;
params.ibd = hbr->getId();
params.ibkey = "abdef";
params.version = "2";
params.track = "false"; //SIGSEGV segmantation fault here
params.cbFn = stationModelAvailableCB;
params.resp = &g_StationModel;
}
void deviceRegisteredCB(QString ibd){
qDebug() << TAG << "Device Registered: ibd = " << ibd;
getStationModel(currentPublicStationID);
}
// End of verbatim code
最后,registerDevice
的实现:
void HRAD::registerDevice(REGISTER_DEVICE_PARAMS_t* params) {
params->cbFn("some ibd");
}
当将这三个部分放入 main.cpp
时,将编译、运行并正确调用回调。即:
Starting bad-c-aargh-31542746...
HRAD_CONTROLLER: Device Registered: ibd = "some ibd"
bad-c-aargh-31542746 exited with code 0
您似乎正在使用回调并通过指针传递值,就像您在 C 中所做的那样。我们将摆脱 C++ 中不必要的 C typedef,并且我们将利用 C++11 的功能来实现回调更有用。
如上,下面的代码只是穿插了注释。您可以将其复制粘贴到 main.cpp
,删除注释,编译并运行。完成了。
首先,让我们包含 functional
header 以引入 std::function
,我们将摆脱 C 风格的宏:
#include <QString>
#include <QDebug>
#include <functional>
const QString TAG = QStringLiteral("HRAD_CONTROLLER:");
const QString HRAD_API_VERSION = QStringLiteral("2");
const QString MANUFACTURER_KEY = QStringLiteral("abcde12345");
然后我们将定义用于将参数传递给 HRAD
方法的结构,并从这些方法中获取响应:
struct RegisterDeviceParams {
QString brand;
QString device;
QString manufacturer;
QString model;
QString serial;
QString ibd;
QString ibkey;
QString version;
QString track;
};
struct GetStationModelParams {
int stationID;
QString ibd;
QString ibkey;
QString version;
QString track;
};
struct GetStationModelReponse {};
然后我们可以实现骨架 HRAD
类。参数作为 const 引用传递。 可选 回调可以是兼容的 lambda、仿函数实例、函数指针等。默认构造值的提供使它们成为可选的。您可以将它们从调用中删除,编译器将使用默认值。 currentPublicStationID
,大概是一个全局变量,也属于 HRAD
。
class HRAD {
public:
void registerDevice(const RegisterDeviceParams &,
const std::function<void(const QString & ibd)> & cb
= std::function<void(const QString&)>()) {
cb("some ibd");
}
void getStationModel(const GetStationModelParams &,
const std::function<void(const GetStationModelReponse &)> & cb
= std::function<void(const GetStationModelReponse&)>()) {
GetStationModelReponse response;
cb(response);
}
QString getId() { return "some id"; }
int currentPublicStationID;
HRAD() : currentPublicStationID(1) {}
};
HRAD
的全局实例是使用 Q_GLOBAL_STATIC
宏定义的。它将在首次使用时以线程安全的方式实例化。它充当指针 - 要获取全局实例,您应该使用 ->
运算符。
Q_GLOBAL_STATIC(HRAD, hbr) // no semicolon needed
getStationModel
和 registerDevice
函数将使用 lambda 语法在现场实例化回调。当然,如果回调更复杂,您可以将它们放在独立的函数或方法中。
void getStationModel(int id){
GetStationModelParams params;
params.stationID = id;
params.ibd = hbr->getId();
params.ibkey = "abdef";
params.version = "2";
params.track = "false";
hbr->getStationModel(params, [](const GetStationModelReponse&){
qDebug() << "got station model";
});
}
void registerDevice(){
RegisterDeviceParams params;
params.brand = "brand";
params.device = "TI-J6";
params.manufacturer = "manuf";
params.model = "EVK";
params.ibkey = MANUFACTURER_KEY;
params.serial = "abcde12345";
params.version = HRAD_API_VERSION;
params.track = "true";
hbr->registerDevice(params, [](const QString & ibd){
qDebug() << TAG << "Device Registered: ibd = " << ibd;
getStationModel(hbr->currentPublicStationID);
});
}
最后,我们从 main 中调用 registerDevice
:
int main()
{
registerDevice();
return 0;
}
此代码有效,并产生以下输出:
Starting bad-c-aargh-31542746...
"HRAD_CONTROLLER:" Device Registered: ibd = "some ibd"
got station model
bad-c-aargh-31542746 exited with code 0
请注意没有任何手动内存管理。原始指针也不被使用——既不作为参数,也不作为成员。
关于c++ - Qt Qstring <not accessible> 和 SIGSEGV 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31542746/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!