- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请注意,问题已确定,请参阅第二部分了解当前详细信息,参阅第三部分了解最可能的原因以及如何补救的问题
我正在用 Qt 编写程序,几天前我因段错误问题而停止。它似乎在提示没有分配内存。找了一圈也想不通为什么会出现分配问题,就休息了。现在,再看一遍,段错误仍然存在,但它在一个完全不同的、几乎不相关的函数中。是什么导致了这些随机段错误?
具体来说,在这种情况下,我目前在这里遇到段错误:
Q_DECL_CONSTEXPR inline int QSize::height() const
{ return ht; }
在我注释掉这些行(最后三行)之前我已经得到它了
qDebug() << QString("---SETTINGS DEBUG---")+QString("\r\n")<<
"netProcPage: "+netProcPage.url()+"\r\n" <<
"mapSize.width(): "+QString::number(mapSize.width())+"\r\n"<<
"mapSize.height(): "+QString::number(mapSize.height())+"\r\n"<<
"mapZoom: "+QString::number(mapZoom)+"\r\n"<<
"mappxSize.width(): "+QString::number(mappxSize.width())+"\r\n"<<
"mappxSize.height(): "+QString::number(mappxSize.height())+"\r\n"<<
"UserCoords[0]: "+QString::number(UserCoords[0])+"\r\n"<<
"UserCoords[1]: "+QString::number(UserCoords[1])+"\r\n"<<
"mapCoordOffsets[0]: "+QString::number(mapCoordOffsets[0])+"\r\n"<<
"mapCoordOffsets[1]: "+QString::number(mapCoordOffsets[1])+"\r\n"<<
"getWindowSize.width(): "+QString::number(getWindowSize.width())+"\r\n"<<
"getWindowSize.height(): "+QString::number(getWindowSize.height())+"\r\n"<<
"mappxOffsets[0]: "+QString::number(mappxOffsets[0])+"\r\n"<<
"mappxOffsets[1]: "+QString::number(mappxOffsets[1])+"\r\n"<<
QString("---END SETTINGS DEBUG---")+QString("\r\n");
在此之前,无需更改任何内容(只需等待几天并稍后重新启动),它就在这里:
mkeMap.genMap(QString("Map1"), tempmapSize, tempmapZoom, mapDisp->ui);
在MainWindow
类构造函数提示 tempmapSize,它由以下定义:
QSize tempmapSize;
tempmapSize = settings->mapSize; //<--- The error might be coming from here, is there an alternative?
这是与段错误相关联的地方。这是设置类:
#include "settings.h"
Settings::Settings(QWidget *parent)
{
netProcPage = "http://localhost:81";
// Max image size is 32767x32767 pixels (divided by 4*mapZoom since 4 panes are used at mapZoom zoom)
// If max mapZoom is 20, max size of map is 409x409, or 408x408 to keep it even
mapSize.setWidth(250);
mapSize.setHeight(250);
mapZoom = 10;
mappxSize.setWidth(mapSize.width()*mapZoom);
mappxSize.setHeight(mapSize.height()*mapZoom);
//downloadMap(netProcPage,"getMap","Username","Password");
//makeMap("bingbong",mapSize,mapZoom);
UserCoords[0] = 0;
UserCoords[1] = 0;
mapCoordOffsets[0] = UserCoords[0] + .5 * mapSize.width();
mapCoordOffsets[1] = UserCoords[1] + .5 * mapSize.height();
//getWindowSize.setWidth(parent->width());
//getWindowSize.setHeight(parent->height());
getWindowSize.setWidth(500);
getWindowSize.setHeight(500);
mappxOffsets[0] = UserCoords[0]*mapZoom + .5 * getWindowSize.width() - .5 * mappxSize.width();
mappxOffsets[1] = UserCoords[1]*mapZoom + .5 * getWindowSize.height() - .5 * mappxSize.height();
}
void Settings::debug()
{
qDebug() << QString("---SETTINGS DEBUG---")+QString("\r\n")<<
"netProcPage: "+netProcPage.url()+"\r\n" <<
"mapSize.width(): "+QString::number(mapSize.width())+"\r\n"<<
"mapSize.height(): "+QString::number(mapSize.height())+"\r\n"<<
"mapZoom: "+QString::number(mapZoom)+"\r\n"<<
"mappxSize.width(): "+QString::number(mappxSize.width())+"\r\n"<<
"mappxSize.height(): "+QString::number(mappxSize.height())+"\r\n"<<
"UserCoords[0]: "+QString::number(UserCoords[0])+"\r\n"<<
"UserCoords[1]: "+QString::number(UserCoords[1])+"\r\n"<<
"mapCoordOffsets[0]: "+QString::number(mapCoordOffsets[0])+"\r\n"<<
"mapCoordOffsets[1]: "+QString::number(mapCoordOffsets[1])+"\r\n"<<
"getWindowSize.width(): "+QString::number(getWindowSize.width())+"\r\n"<<
"getWindowSize.height(): "+QString::number(getWindowSize.height())+"\r\n";//<<
//"mappxOffsets[0]: "+QString::number(mappxOffsets[0])+"\r\n"<<
//"mappxOffsets[1]: "+QString::number(mappxOffsets[1])+"\r\n"<<
//QString("---END SETTINGS DEBUG---")+QString("\r\n");
}
QSize* Settings::getmapSize()
{
return &mapSize;
}
int Settings::getmapZoom()
{
return mapZoom;
}
我已经按照建议重构了代码,并查明了确切的问题,但我不知道如何解决它。
void makeMap::genMap(QString name, QPointF* inSize, int* zoom, Ui::MapDisp* ui)
{
QVector<QString> mapvector;
QPointF mapSize = *inSize; // <--- The problem is right here
...
}
取消引用 QPointF
时会出现问题在 Settings
的早期实例中找到的对象,已发送至 genMap(...)
.
调用是这样完成的:
QPointF* tempmapSize;
tempmapSize = settings->getmapSize();
int* tempmapZoom = settings->getmapZoom();
mkeMap.genMap(QString("Map1"), tempmapSize, tempmapZoom, mapDisp->ui);
无论我将取消引用( *Settings::inSize*
或 *inSize
)移到哪里,这就是错误发生的地方(在调试器中)。一切都编译得很好。
运行程序时,它崩溃并出现以下错误:
Starting C:\program-debug.exe...
ASSERT failure in QVector<T>::operator[]: "index out of range", file ../../../../../Qt/5.2.0/mingw48_32/include/QtCore/qvector.h, line 369
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.
C:\program-debug.exe exited with code 3
此引用 QVector<T>
指的是后面的 genMap
,
void makeMap::genMap(QString name, QPointF* inSize, int* zoom, Ui::MapDisp* ui)
{
QVector<QString> mapvector;
QPointF mapSize = *inSize; //<---Here's the segmentation fault
/* Using this instead of the above works, as well as replacing zoom which causes another segmentation fault when dereferenced
QPointF mapSize;
mapSize.setX(250);
mapSize.setY(250);
int zoom0 = 10;
*/
QFile file(name+"_"+QString::number(mapSize.x())+"x"+QString::number(mapSize.y())+".rtsmap");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
mapvector.resize(mapSize.x() * mapSize.y());
for(int x = 0; x < mapSize.x(); x++){
for(int y = 0; y < mapSize.y(); y++){
uint decimalcolor = (((qSin(x)+1) + (qSin(y)+1))/4)>1?16777215:(((qSin(x)+1) + (qSin(y)+1))/4)*16777214;
QString hexadecimalcolor;
hexadecimalcolor.setNum(decimalcolor,16);
mapvector[index(x, y, mapSize)] = "#" + hexadecimalcolor;
//drawRect(x*10,y*10,10,10,"#"+hexadecimalcolor,zoom);
out << "#" << hexadecimalcolor+'\n';
}
}
file.close();
drawMap(mapvector,zoom0,ui,mapSize);
}
我认为问题是悬挂指针。更具体地说,当我将设置指针传递给类构造函数时,这里:
MapCtrl::MapCtrl(QWidget *parent, Settings *settingsIn) :
QWidget(parent),
ui(new Ui::MapCtrl)
{
ui->setupUi(this);
mapDisp = new MapDisp(parent, settingsIn);
addMap();
settings = settingsIn;
}
指针settingsIn
可能在构造函数的末尾用 settings
删除了仍然指向那里,所以稍后当我从 settings
中取消引用一个值时,它不存在,导致段错误。所以,问题是,我如何阻止名为 settingsIn
的指针?在构造函数末尾被删除?
请求代码
这是 MapCtrl
的位置构造器 MapCtrl::MapCtrl
被调用并且 Settings
被实例化:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
wScr = new WelcomeScreen(this);
Settings *settings = new Settings(this);
mapCtrl = new MapCtrl(parent,settings);
...
}
解决方案(感谢 Kuba Ober 的指导和大量有用的 C++ 知识)
除了检查悬空指针和修复一些可能的原因之外,最后一步是改变它:
MapCtrl::MapCtrl(QWidget *parent, Settings *settingsIn) :
QWidget(parent),
ui(new Ui::MapCtrl)
{
ui->setupUi(this);
mapDisp = new MapDisp(parent, settingsIn);
addMap();
qDebug() << "bingbong!!" << settingsIn->mapSize.x();
settings = settingsIn;
}
为此:
MapCtrl::MapCtrl(QWidget *parent, Settings *settingsIn) :
QWidget(parent),
ui(new Ui::MapCtrl)
{
ui->setupUi(this);
mapDisp = new MapDisp(parent, settingsIn);
qDebug() << "bingbong!!" << settingsIn->mapSize.x();
settings = settingsIn;
addMap();
}
settings
在需要 settings
的函数之后设置待设置。
最佳答案
在你的整个代码库中:
去掉通过指针返回 Qt 容器(QSize
、QList
等)的地方,代之以按值返回它们。
声明访问器方法为常量。
例如将 Settings::getmapSize()
更改为以下惯用代码:
QSize Settings::getmapSize() const
{
return mapSize;
}
使用 QPointF
作为坐标,而不是裸数组。通过值或 const 引用将它们传递给方法/函数;前者实际上可能在现代硬件和编译器上运行得更快。
例如将构造函数中的代码更改为如下所示:
mapCoordOffsets = UserCoords + (toPoint(mapSize) * .5);
在哪里
static QPointF toPoint(const QSize & size) {
return QPointF(size.width(), size.height());
}
摆脱 C 风格的输出参数,就像在
void makeMap::genMap(..., QPointF* inSize, int* zoom, ...)
如果你要返回一些东西,只需返回一个被调用的结构,比如 MapParams
或类似的东西:
MapParams makeMap::genMap(...);
如果您正在获取一个结构并可能对其进行修改,只需对其进行修改并返回修改后的结构:
MapParams makeMap::genMap(..., MapParams params) {
...
params.size = ...;
params.zoom = ...;
...
return params;
}
我担心的是,您在某个地方传递了一个伪造的指针,或者指针混淆了一个生命周期问题。现代 C++ 中的裸指针非常可疑,您的代码似乎毫无理由地使用了很多裸指针。用指针实现的输出参数是 C-ism,在现代 C++ 中没有立足之地。
另一个常见错误可能是这样的:您在 MainWindow 类中有一个 Settings * settings 成员。然后,您愉快地在 MainWindow
构造函数中创建了一个 Settings *
局部变量,但是 MainWindow::settings
成员仍未初始化。这就是为什么您应该始终在成员名称前加上 m_
,这样它们就更难与局部变量混淆。理想情况下,您应该使用初始化列表来初始化构造函数中的成员。
在调试的时候,当你看到一个关于越界访问的断言时,你需要调试它的原因。调试器应该停止在这样的断言上,如果不是,那么你有一些配置错误(你的确切平台和工具集是什么?)。如果是写访问,之后发生的任何事情都没有意义,因为内存已被覆盖,此时您正在追逐松鼠。
做这些事情可以减少您在代码中可能犯的一大堆错误。它可能无法解决您的问题,但会让您走上通往解决方案的良好轨道。
关于c++ - 随机段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22646262/
我已经使用 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
我是一名优秀的程序员,十分优秀!