尝试从类中更改字符串时QT崩溃

时间:2013-01-24 06:46:06

标签: qt qt-creator

以下是我的脚本:

void FileOperations::SetInputFile(QString x){
    InputFilename=x.toStdString();
}
void FileOperations::SetOutputFile(QString x){
    OutputFilename=x.toStdString();
}

尝试将InputFilename设置为x时,第一个(SetInputFile)崩溃。 第二个工作正常。

这是我正在改变这两个字符串的类:

class FileOperations
{
public:
    void SetInputFile(QString x);
    void SetOutputFile(QString x);
private:
    string InputFilename;
    string OutputFilename;
};

如您所见,'InputFilename'和'OutputFilename'是相同的,它们的功能也几乎相同。 我尝试将字符串更改为不同的名称,但它仍然崩溃。

崩溃后我可以按调试,查看Microsoft Visual Studio 2010中的错误,这里是崩溃信息:

  

msvcr100d.dll!operator delete(void * pUserData)第52行+ 0x3字节C ++       ShitLEncoder.exe!std :: allocator :: deallocate(char * _Ptr,unsigned int __formal)第182行+ 0x9字节C ++       ShitLEncoder.exe!std :: basic_string,std :: allocator> :: _ Tidy(bool _Built,unsigned int _Newsize)1998行C ++       ShitLEncoder.exe!std :: basic_string,std :: allocator> :: assign(std :: basic_string,std :: allocator>&& _Right)Line 722 C ++       ShitLEncoder.exe!std :: basic_string,std :: allocator> :: operator =(std :: basic_string,std :: allocator>&& _Right)Line 710 C ++       ShitLEncoder.exe!FileOperations :: SetInputFile(QString x)第8行+ 0x26字节C ++       ShitLEncoder.exe!MainWindow :: on_OpenButton_clicked()第20行C ++       ShitLEncoder.exe!MainWindow :: qt_static_metacall(QObject * _o,QMetaObject :: Call _c,int _id,void * * _a)第72行+ 0x8字节C ++       ShitLEncoder.exe!MainWindow :: qt_metacall(QMetaObject :: Call _c,int _id,void * * _a)106行+ 0x15字节C ++       Qt5Cored.dll!615820e0()
      [下面的框架可能不正确和/或缺失,没有为Qt5Cored.dll加载符号]
      Qt5Cored.dll!615c613f()
      Qt5Cored.dll!615c5ac1()
      Qt5Widgetsd.dll!66a7118b()
      Qt5Widgetsd.dll!66727087()
      Qt5Widgetsd.dll!6672703b()
      Qt5Widgetsd.dll!66728092()
      Qt5Widgetsd.dll!66629ef5()
      Qt5Widgetsd.dll!66624c40()
      Qt5Widgetsd.dll!665e0887()
      Qt5Widgetsd.dll!66625405()
      Qt5Widgetsd.dll!6665de30()
      Qt5Widgetsd.dll!665f15a0()
      Qt5Widgetsd.dll!665e0253()
      msvcr100d.dll!_free_dbg(void * pUserData,int nBlockUse)1267行+ 0xc字节C ++       0246b188()

它在代码中显示的第一件事是:

_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

我正在使用QT 5和QT Creator。

[编辑] 以下是QT主窗口类用于调用这些函数的代码:

void MainWindow::on_OpenButton_clicked()
{
    ui->OpenText->setText(QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Any                               file")));
    FileController->SetInputFile(ui->OpenText->text());
}

void MainWindow::on_SaveButton_clicked()
{
    ui->SaveText->setText(QFileDialog::getSaveFileName(this, tr("Save File"),"",tr("Any     file")));
    FileController->SetOutputFile(ui->SaveText->text());
}

1 个答案:

答案 0 :(得分:0)

好的,我修好了,我不确定这是如何运作的,我改变了这个:

class FileOperations
{
public:
    void SetInputFile(QString x);
    void SetOutputFile(QString x);
private:
    string InputFilename;
    string OutputFilename;
};

对此:

class FileOperations
{
public:
    void SetInputFile(QString x);
    void SetOutputFile(QString x);
private:
    string UnusedVariable;
    string InputFilename;
    string OutputFilename;
};

我添加了一个额外的未使用变量,它会停止崩溃:D