在使用Qt创建器创建一个非常简单的项目时,在Qt中遇到“分段错误”

时间:2015-04-29 09:33:26

标签: c++ qt qt4

以下是我的源代码,超级简单的一个:

#include<QApplication>
#include<QLabel>
int main(int argc,char* argv[])
{
   QApplication app(argc,argv);
   QLabel *label=new QLabel("Hello world");
   label->show();
   return app.exec();
}

当我建造它时,一切都很好。但是当我尝试运行它时,它立即停止,告诉我“* .exe已经停止。关闭它或调试它。”。

所以我尝试调试它。过了一会儿,它会抛出一条消息:

The inferior stopped because it received a signal from the operating system

 SINAL NAME: SIGSEGV
 SIGNAL MEANING: segmentation default

我在google和stackoverflow以及维基百科上检查了很多。很多人说它是由错误使用指针引起的。但我无法找出问题所在。请帮忙。

顺便说一下,我正在使用win7,我在qt 4.8.5和qt 5.4.0上尝试过。 编译器和调试器都很好(至少qt创建者没有显示错误)。

我应该重新安装Qt吗?

这是错误的代码(在 qatomic_i386.h 中)调试器为我指出:

inline bool QBasicAtomicInt::deref()
{
        unsigned char ret;
    asm volatile("lock\n"
                 "decl %0\n"
                 "setne %1"
                 : "=m" (_q_value), "=qm" (ret)
                 : "m" (_q_value)

                 : "memory");   //this is the wrong code the debugger point out for me

 return ret != 0;
}

qstring.h 中的另一行:

inline QString::~QString() { if (!d->ref.deref()) free(d); }

这都是错误的信息。感谢。

*****************代码更改***但是相同的******

#include<QApplication>
#include<QLabel>
#include<QDialog>

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    QDialog* dialog=new QDialog;
    QLabel* label=new QLabel(dialog);
    label->setText("Hello world");
    dialog->show();
    //label->show();    it's the same with or without this

    return app.exec();
}

***********更多但是改变了*********

#include<QApplication>
#include<QLabel>
#include<QDialog>

int main(int argc,char* argv[])
{
    QApplication app(argc,argv);
    QDialog dialog;
    QLabel label(&dialog);
    label.setText("Hello world");
    dialog.show();
    //label->show();

    return app.exec();
}

1 个答案:

答案 0 :(得分:0)

请检查您未尝试使用控制台应用程序的.pro文件,必须使用qwidet应用程序。:

#-------------------------------------------------
#
# Project created by QtCreator 2015-05-04T11:12:27
#
#-------------------------------------------------

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets


TARGET = testapp


TEMPLATE = app


SOURCES += main.cpp

应用程序对我的代码运行良好,,,它运行procces_stub.exe cmd然后它在窗口上显示标签......