gpt4 book ai didi

c++ - 随机段错误?

转载 作者:行者123 更新时间:2023-11-28 00:31:43 25 4
gpt4 key购买 nike

请注意,问题已确定,请参阅第二部分了解当前详细信息,参阅第三部分了解最可能的原因以及如何补救的问题

我正在用 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 的函数之后设置待设置。

最佳答案

在你的整个代码库中:

  1. 去掉通过指针返回 Qt 容器(QSizeQList 等)的地方,代之以按值返回它们。

  2. 声明访问器方法为常量。

    例如将 Settings::getmapSize() 更改为以下惯用代码:

    QSize Settings::getmapSize() const
    {
    return mapSize;
    }
  3. 使用 QPointF 作为坐标,而不是裸数组。通过值或 const 引用将它们传递给方法/函数;前者实际上可能在现代硬件和编译器上运行得更快。

    例如将构造函数中的代码更改为如下所示:

    mapCoordOffsets = UserCoords + (toPoint(mapSize) * .5);

    在哪里

    static QPointF toPoint(const QSize & size) {
    return QPointF(size.width(), size.height());
    }
  4. 摆脱 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++ 中没有立足之地。

  5. 另一个常见错误可能是这样的:您在 MainWindow 类中有一个 Settings * settings 成员。然后,您愉快地在 MainWindow 构造函数中创建了一个 Settings * 局部变量,但是 MainWindow::settings 成员仍未初始化。这就是为什么您应该始终在成员名称前加上 m_,这样它们就更难与局部变量混淆。理想情况下,您应该使用初始化列表来初始化构造函数中的成员。

在调试的时候,当你看到一个关于越界访问的断言时,你需要调试它的原因。调试器应该停止在这样的断言上,如果不是,那么你有一些配置错误(你的确切平台和工具集是什么?)。如果是写访问,之后发生的任何事情都没有意义,因为内存已被覆盖,此时您正在追逐松鼠。

做这些事情可以减少您在代码中可能犯的一大堆错误。它可能无法解决您的问题,但会让您走上通往解决方案的良好轨道。

关于c++ - 随机段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22646262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com