ldcsaa / HP-Socket

High Performance TCP/UDP/HTTP Communication Component

Home Page:https://www.oschina.net/p/hp-socket

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hpsocket和qt操作sqlite不兼容吗?

nblwdlt opened this issue · comments

我才用vcpkg安装的qt5-base,测试工程代码如下:
`#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_qtwidgetsapplication3.h"
#include "SocketInterface.h"
#include "HPSocket.h"

class QtWidgetsApplication3 : public QMainWindow ,public CTcpServerListener
{
Q_OBJECT

public:
QtWidgetsApplication3(QWidget *parent = nullptr);
~QtWidgetsApplication3();

private:
virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen);
virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient);
virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode);
virtual EnHandleResult OnShutdown(ITcpServer* pSender);
private:
Ui::QtWidgetsApplication3Class ui;
CTcpPackServerPtr m_Server;
};
#include "qtwidgetsapplication3.h"
#include
#include
#include
#include

QtWidgetsApplication3::QtWidgetsApplication3(QWidget *parent)
: QMainWindow(parent), m_Server(this)
{
ui.setupUi(this);

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mydb.db");

// 打开数据库
if (!db.open()) {
  qDebug() << "Error: Unable to open database" << db.lastError();
  return;
}

// 创建表
QSqlQuery query;
if (!query.exec("CREATE TABLE person (id INTEGER PRIMARY KEY, name TEXT)")) {
  qDebug() << "Error: Unable to create table" << query.lastError();
  return;
}

// 插入数据
query.prepare("INSERT INTO person (name) VALUES (:name)");
query.bindValue(":name", "John Doe");
if (!query.exec()) {
  qDebug() << "Error: Unable to insert data" << query.lastError();
  return;
}

query.bindValue(":name", "Jane Doe");
if (!query.exec()) {
  qDebug() << "Error: Unable to insert data" << query.lastError();
  return;
}

// 查询数据
if (!query.exec("SELECT id, name FROM person")) {
  qDebug() << "Error: Unable to execute query" << query.lastError();
  return;
}

while (query.next()) {
  int id = query.value("id").toInt();
  QString name = query.value("name").toString();
  qDebug() << "Person:" << id << name;
}
db.close();

}

QtWidgetsApplication3::~QtWidgetsApplication3()
{}

EnHandleResult QtWidgetsApplication3::OnPrepareListen(ITcpServer * pSender, SOCKET soListen)
{
return EnHandleResult();
}

EnHandleResult QtWidgetsApplication3::OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient)
{
return EnHandleResult();
}

EnHandleResult QtWidgetsApplication3::OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
return EnHandleResult();
}

EnHandleResult QtWidgetsApplication3::OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength)
{
return EnHandleResult();
}

EnHandleResult QtWidgetsApplication3::OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode)
{
return EnHandleResult();
}

EnHandleResult QtWidgetsApplication3::OnShutdown(ITcpServer* pSender)
{
return EnHandleResult();
}
`
这会在db.open的时候导致程序崩溃。

需要补充一下,我是用的是x86-static

这个问题好像是HPSocket使用malloc和系统的冲突了,需要禁用

这个问题好像是HPSocket使用malloc和系统的冲突了,需要禁用

能说下具体操作吗?十分感谢。

目前暂时的解决办法就是使用hpsocket.dll放弃lib。

这个问题好像是HPSocket使用malloc和系统的冲突了,需要禁用

能说下具体操作吗?十分感谢。

项目属性 -> 链接 -> 输入,去掉 mimalloc,或者改为 jemalloc 试试。