Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hpsocket和qt操作sqlite不兼容吗? #250

Open
nblwdlt opened this issue Apr 15, 2023 · 6 comments
Open

hpsocket和qt操作sqlite不兼容吗? #250

nblwdlt opened this issue Apr 15, 2023 · 6 comments

Comments

@nblwdlt
Copy link

nblwdlt commented Apr 15, 2023

我才用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的时候导致程序崩溃。

@nblwdlt
Copy link
Author

nblwdlt commented Apr 15, 2023

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

@nblwdlt
Copy link
Author

nblwdlt commented Apr 15, 2023

Snipaste_2023-04-15_10-25-17

@Endt4sk
Copy link

Endt4sk commented Apr 15, 2023

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

@nblwdlt
Copy link
Author

nblwdlt commented Apr 15, 2023

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

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

@nblwdlt
Copy link
Author

nblwdlt commented Apr 16, 2023

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

@ldcsaa
Copy link
Owner

ldcsaa commented Apr 27, 2023

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants