Qt C++函数中断删除未知变量

Qt C++ function breaks deleting unknown variable

本文关键字:未知 变量 删除 中断 C++ 函数 Qt      更新时间:2023-10-16

我有一个简单的gui,它有一个文本字段、一个下拉菜单和一个go按钮。我可以指定我要查找的部件的名称和类,并通过将"go"按钮连接到运行我已经创建的函数的插槽来调用函数。

然而,当slot函数处理完所有内容时,它会调用xstring中的一个函数,即删除一些大量的xstring。它转到这个功能:

void _Tidy(bool _Built = false,
    size_type _Newsize = 0)
    {   // initialize buffer, deallocating any storage
    if (!_Built)
        ;
    else if (this->_BUF_SIZE <= this->_Myres)
        {   // copy any leftovers to small buffer and deallocate
        pointer _Ptr = this->_Bx._Ptr;
        this->_Getal().destroy(&this->_Bx._Ptr);
        if (0 < _Newsize)
            _Traits::copy(this->_Bx._Buf,
                _STD addressof(*_Ptr), _Newsize);
        this->_Getal().deallocate(_Ptr, this->_Myres + 1);
        }
    this->_Myres = this->_BUF_SIZE - 1;
    _Eos(_Newsize);
}

我的程序在this->_Getal().deallocate(_Ptr, this->_Myres + 1);执行一个中断。

这是gui的代码:

#include <QtGui>
#include <QApplication>
#include <QComboBox>
#include "gui.h"
#include <vector>
std::vector<std::string> PartClasses;
gui::gui(QWidget *parent) : QDialog(parent){
    getPartClasses(PartClasses); //my own function, does not affect how the gui runs, just puts strings in PartClasses
    label1 = new QLabel(tr("Insert Name (Optional):"));
    label2 = new QLabel(tr("Class Name (Required):"));
    lineEdit = new QLineEdit;
    goButton = new QPushButton(tr("&Go"));
    goButton->setDefault(true);
    connect(goButton, SIGNAL(clicked()), this, SLOT(on_go_clicked()));
    cb = new QComboBox();
    for(int i = 0; i < PartClasses.size(); i++)
        cb->addItem(QString::fromStdString(PartClasses[i]));
    //*add widgets to layouts, removed for space*
    setWindowTitle(tr("TEST"));
    setFixedHeight(sizeHint().height());
}
void gui::on_go_clicked(){
    std::string str(cb->currentText().toStdString());
    updateDB(str, lineEdit->text().toUtf8().constData()); //my function, does not affect the gui.
    QApplication::exit();
}
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    gui *stuff = new gui;
    stuff->show();
    return app.exec();
}

它在做什么?当我完成了这个插槽时,gui不应该重新出现,这样我就可以指定一个新的对象吗?我如何才能使它不删除此对象,或者使它成功?

以下是我对正在发生的事情的最佳猜测:

您正在访问的对象正在某个不该删除的地方被删除。

_Tidy函数看起来像是在字符串操作之后进行一些清理。很可能你的char *的常量没有降下来,你正在删除一个常量指针。

为了解决这个问题,我会对您的变量进行深度复制,并将其传递到执行xstringLaTex的updateDB中。或者,您可以立即创建一个xstring对象并将其传递下去。

我也会考虑使用strcpy或类似的东西,或者可能只是std::string

此外,出现的崩溃代码也会有所帮助。

编辑:

以下是您的代码可能应该是什么样子。。。

void gui::on_go_clicked(){
    std::string str(cb->currentText().toStdString());
    std::string line_edit_str(lineEdit->text().toUtf8().constData());
    updateDB(str, line_edit_str); //my function, does not affect the gui.
    QApplication::exit();
}

希望能有所帮助。