qthread + sync qhttp = memory read error

Alles rund um die Programmierung mit Qt
Antworten
nini_knoxville
Beiträge: 29
Registriert: 1. März 2007 16:07
Kontaktdaten:

qthread + sync qhttp = memory read error

Beitrag von nini_knoxville »

hi,

ich schreibe z.z. an einer klitzekleinen anwendung, um mich in threads einzuarbeiten. meine anwendung soll per buttonklick einen thread starten und in diesem thread per synchronen qhttp eine datei runterladen und den dateiinhalt der anwendung emitten.
das mit dem thread und dem emitten klappt auch. nur wenn ich den qhttp download rein packe, passiert nix. sobald ich irgendwo in die anwendung klicke bekomme ich einen windows memory read error bla blub.
ich weiss echt nich mehr weiter. die syncqhttp klasse funktioniert sonst tadelos.

hier die files:
workthread.h

Code: Alles auswählen

#ifndef RENDERTHREAD_H
#define RENDERTHREAD_H

#include <QThread>
#include <QString>

#include "syncqhttp.h"

class WorkThread : public QThread
{
    Q_OBJECT

public:
	WorkThread(QObject *parent = 0);
    ~WorkThread();

    void download();

signals:
    void working(QString str, int current, int max);
    void data(QString data);

protected:
    void run();

private:
    SyncQHTTP *http;
};

#endif
workthread.cpp

Code: Alles auswählen

#include "workthread.h"

#include <QBuffer>

WorkThread::WorkThread(QObject *parent) :
	QThread(parent)
{
	http = new SyncQHTTP("localhost");
}

WorkThread::~WorkThread()
{
	delete http;
	http = 0;
}

void WorkThread::download()
{
	if (!isRunning())
	{
		start(LowPriority);
	}
}

void WorkThread::run()
{
	emit working(QString("im working on file 1"), 0, 1);

	QBuffer buffer;
	http->syncGet("/1.txt", &buffer);

	emit data(QString(buffer.data()));
}
test.h

Code: Alles auswählen

#ifndef TEST_H
#define TEST_H

#include <QtGui/QMainWindow>
#include <QString>
#include "ui_test.h"
#include "workthread.h"

class test : public QMainWindow
{
    Q_OBJECT

public:
    test(QWidget *parent = 0);
    ~test();

private slots:
	void updateProgressBar(QString str, int current, int max);
	void setText(QString str);
	void startThread();

private:
	WorkThread thread;
    Ui::testClass ui;
};

#endif
test.cpp

Code: Alles auswählen

#include "test.h"

#include "syncqhttp.h"

test::test(QWidget *parent) :
	QMainWindow(parent)
{
	ui.setupUi(this);

	connect(&thread, SIGNAL(working(QString, int, int)), this, SLOT(updateProgressBar(QString, int, int)));
	connect(&thread, SIGNAL(data(QString)), this, SLOT(setText(QString)));
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(startThread()));

	ui.progressBar->hide();
	ui.label->hide();
	ui.progressBar->setMinimum(0);
	ui.progressBar->setMaximum(1);
	ui.progressBar->setValue(0);
}

test::~test()
{

}

void test::updateProgressBar(QString str, int current, int max)
{
	ui.label->setText(str);
	ui.progressBar->setValue(current);
	ui.progressBar->setMaximum(max);
}

void test::setText(QString str)
{
	ui.pushButton->setDisabled(false);
	ui.progressBar->hide();
	ui.label->hide();
	ui.textBrowser->setText(str);
}

void test::startThread()
{
	ui.pushButton->setDisabled(true);
	ui.progressBar->show();
	ui.label->show();

	thread.download(););
}
und synqhttp.h

Code: Alles auswählen

/***************************************************************************
 *   Copyright (C) 2005 by Iulian M                                        *
 *   eti@erata.net                                                         *
 ***************************************************************************/
#ifndef SYNCQHTTP_H
#define SYNCQHTTP_H

#include <QHttp>
#include <QEventLoop>
#include <QBuffer>

class SyncQHTTP : public QHttp
{
Q_OBJECT
public:
	SyncQHTTP(QObject *parent = 0) : QHttp(parent), requestID(-1), status(false)
	{}

	SyncQHTTP(const QString &hostName, quint16 port = 80, QObject *parent = 0) : QHttp(hostName, port, parent), requestID(-1), status(false)
	{}

	virtual ~SyncQHTTP()
	{}

	bool syncGet(const QString &path, QIODevice *to = 0)
	{
		connect(this, SIGNAL(requestFinished(int, bool)), SLOT(finished(int, bool)));
		requestID = get(path, to);
		loop.exec();
		return status;
	}

	bool syncPost(const QString &path, QIODevice *data, QIODevice *to = 0)
	{
		connect(this, SIGNAL(requestFinished(int, bool)), SLOT(finished(int, bool)));
		requestID = post(path, data , to);
		loop.exec();
		return status;
	}

	bool syncPost(const QString &path, const QByteArray &data, QIODevice *to = 0)
	{
		QBuffer buffer;
		buffer.setData(data);
		return syncPost(path, &buffer, to);
	}

protected slots:
	virtual void finished(int index, bool error)
	{
		if(index != requestID)
		{
			return;
		}

		status = !error;
		loop.exit();
	}

private:
	int requestID;
	bool status;
	QEventLoop loop;
};

#endif
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Beitrag von Christian81 »

Dein hhtp-Objekt lebt im Main-Thread, nicht in deinem neu erzeugten Thread.
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
nini_knoxville
Beiträge: 29
Registriert: 1. März 2007 16:07
Kontaktdaten:

Beitrag von nini_knoxville »

ja.. komischerweise gehts hier zuhause unter linux einwandfrei.. auf der arbeit unter windows gings irgendwie nich.. komisch
Antworten