【问题标题】:Programm crashes when QTcpServer is called调用 QTcpServer 时程序崩溃
【发布时间】:2016-03-20 03:58:24
【问题描述】:

我正在尝试一个非常非常简单的 QT 网络程序。由于某种原因,它在没有任何错误消息的情况下执行时崩溃,因为它没有按预期将任何输出打印到命令行。代码如下:

qtTCPservertest.pro

QT       += core
QT       += network
QT       -= gui

TARGET   = qtTCPservertest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    theserver.cpp

HEADERS += \
    theserver.h

theServer.h

#ifndef THESERVER_H
#define THESERVER_H

#include <QTcpServer>
#include <stdio.h>


class theServer : public QTcpServer{
    Q_OBJECT
public:
    theServer();
    ~theServer();
    void goOnline();
};

#endif // THESERVER_H

theServer.cpp

#include "theserver.h"
theServer::theServer()
{
}

theServer::~theServer()
{
}

void theServer::goOnline()
{
       bool status = false;
       unsigned int portNum = 5200;

       status = this->listen(QHostAddress::Any, portNum );

       // Check, if the server did start correctly or not
       if( status == true )
           printf("Server up\n");
       else
           printf("Server down\n");
}

和 ma​​in.cpp

#include <QCoreApplication>
#include <stdio.h>
#include "theserver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    printf("Test\n");
    theServer* aServer = new theServer();
    aServer->goOnline();
    aServer->~theServer();

    return a.exec();
}

有人知道我哪里出错了吗?由于没有错误,我完全一无所知。它只是没有打印出任何东西,它只是告诉我按任意键关闭窗口,就好像它像往常一样结束了。

感谢您的建议。

【问题讨论】:

  • 您是否尝试在调试中运行它?
  • @cen 现在我在将 gdb 与 Qt/Creator 一起使用时遇到问题,因为我相信修复这将是一项更大的工作,所以我无法调试它(如果你的意思是例如:@987654325 @通过那个)
  • 我建议你在 Qt Creator 中修复调试器,因为至少你会知道程序崩溃的确切位置。一旦你知道它应该更容易。如果没有打印“测试”,那么网络问题会更大。
  • 您的 theServer 类必须是 QObject(缺少 Q_OBJECT 宏)
  • @Archie 没错,但还是一样。我尝试了另一种选择:我的类仅从“公共 QObject”继承,并且我声明了像 QTcpServer* aServer 这样的服务器,直到这里一切都运行顺利,如果我插入初始化错误返回 aServer = new QTcpServer(this) 并且 id 无关紧要无效地调用该构造函数。这就是为什么我仍然认为是网络问题

标签: c++ qt syntax-error runtime-error qtcpserver


【解决方案1】:

这是为我编译和工作的代码(Qt 5.5):

TheServer.h

#ifndef THESERVER_H
#define THESERVER_H

#include <QTcpServer>

class TheServer : public QTcpServer
{
    Q_OBJECT
public:
    TheServer(QObject *pParent = nullptr);
    void goOnline();
};

#endif // THESERVER_H

TheServer.cpp

#include <QDebug>
#include "TheServer.h"

TheServer::TheServer(QObject *pParent)
    : QTcpServer(pParent)
{
}

void TheServer::goOnline()
{
    bool status = listen(QHostAddress::Any, 5200);

    if (status) {
        qDebug() << "Server up";
    } else {
        qDebug() << "Server down";
    }
}

ma​​in.cpp

#include <QCoreApplication>
#include "TheServer.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    TheServer server;
    server.goOnline();

    return a.exec();
}

【讨论】:

  • 首先:感谢您的耐心和帮助。这并不能真正解决我的问题,但它确实表明我走在正确的轨道上并且代码没问题。我没想到会这样,但现在我认为 QTNetwork 库有问题,可能丢失或不兼容。所以我必须调试我在这里给出的 QT 设置,因为它不是一个常见的现成的
  • @user3085931 确保您加载了正确的 Qt 库(例如,如果您有多个 Qt 安装或某些 Qt 相关程序将自身放入 PATH)。在 Windows 上,您可以使用 dependency walker (dependencywalker.com) 查看启动可执行文件时实际加载的 DLL。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多