Krieger910
26th March 2012, 12:38
Hello i am just writing a computer Game , called BattleShips. It's just a computer game with a client,server,client communication where the main communication is just throw a QVector and QTcpsockets,and i am sending these QVectors with an overloaded Function.
This is the Function where i send my QVector to the Server.
void Client::sendPlayground(){
// QByteArray wird definiert
QByteArray block;
// QDataStream zum Schreiben in das QByteArray
QDataStream out(&block, QIODevice::WriteOnly);
// Version des DataStereams setzen
out.setVersion(QDataStream::Qt_4_5);
// Daten in den Stream schreiben
out << qint16(0)
<< qint8('P')
<< *m_playground;
// Zurueckspulen und
out.device()->seek(0);
// Datenlaenge in Byte schreiben
qint16 blocksize=qint16(block.size() - sizeof(qint16));
qDebug() << "Size of mine Playground " << sizeof(*m_playground);
qDebug() << "Size of Vector 1"<< sizeof(m_playground->m_defArea->getVector());
qDebug() << "Size of Vector 1"<< sizeof(m_playground->m_offArea->getVector());
qDebug() << "Blocksize Playground Client" << blocksize << "Size of DataStream" << block.size();
out << blocksize;
// QByteArray an den Client schicken
m_serverConnection->write(block);
m_serverConnection->flush();
}
Here is my function for overloading my object called Playground , Playground inherits two Objects called GameArea also stores one QVectors for saving some stats.
Serialize
QDataStream& operator <<(QDataStream & os,const Playground& pg ){
os << pg.m_defArea->getSize() << pg.m_defArea->getVector() << pg.m_offArea->getSize() << pg.m_offArea->getVector();
return os;
}
//Deserialize
QDataStream& operator>>(QDataStream & is,Playground& pg ){
QVector<QVector<int> > vec1;
QSize size1;
QVector<QVector<int> > vec2;
QSize size2;
is >> size1 >> vec2 >> size2 >> vec1;
pg.m_offArea->setSize(size1);
pg.m_offArea->setVector(vec1);
qDebug() << "Size 1" << size1;
qDebug() << "Size 2" << size2;
pg.m_defArea->setSize(size1);
pg.m_defArea->setVector(vec2);
return is;
}
and here is my GameArea , this Objects stores a QVector<QVector<int> > m_cells which saves some stats.
#ifndef GAMEAREA_H
#define GAMEAREA_H
#include <QWidget>
#include <vector>
#include <QMouseEvent>
#include "Game.h"
#include <QDebug>
#include <QVector>
#include <QMap>
#include <QObject>
class Client;
class GameArea : public QWidget
{
Q_OBJECT
public:
GameArea(Client* client);
~GameArea();
//KopyConstructor
GameArea(const GameArea &a);
GameArea& operator=(const GameArea&a);
void setCellState(int x,int y, int value);
int getCellState(int x, int y);
bool isCellEmpty(int x, int y);
void setlocked(bool flag);
void clear();
void setShipMode(Shiptype type);
Shiptype getShipMode();
bool createShip(int x, int y);
void loadResources();
//Returns the stored Vector
const QVector<QVector<int> >& getVector() const;
//Returns the size of the Vector
QSize getSize() const;
//sets the new Vector
void setVector(QVector<QVector<int> > vec);
//sets new Size
void setSize(const QSize& size);
void count_ships();//Hits and Fails in the GameArea for Enemy and You
//Get my count ships
QMap<char,int> * get_m_c_ships();
//Get all maximum of all Ships
QMap<char,int> * get_m_m_ships();
//Get my hits and fails
QMap<char,int> * get_m_hit_fails();
//Get hits and fails of the enemy
QMap<char,int> * get_e_hit_fails();
bool isReady();
signals:
void new_Move();
void won();
void lost();
protected:
virtual void paintEvent(QPaintEvent *e);
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
void getRectForCell(int x, int y, QRect& rect);
private:
QPoint m_tagged;
int m_width;
int m_height;
int m_offsetX; // width-offset from margin
int m_offsetY; // height-offset from margin
QVector<QVector<int> > m_cells;
void reserve_m_cells(int rows,int col);
void resize_m_cells(int rows,int col);
bool isInitialized;
bool m_locked;
Client* m_client;
Shiptype m_shipType;
QImage* image;
QMap<char,int> m_c_ships;
QMap<char,int> m_m_ships;
QMap<char,int> m_hit_fails;
QMap<char,int> e_hit_fails;
};
Q_DECLARE_TYPEINFO(GameArea, Q_MOVABLE_TYPE );
Q_DECLARE_TYPEINFO(QVector<QVector<int> >, Q_MOVABLE_TYPE );
#endif // GAMEAREA_H
Here is my function to get this QVector from my Object
const QVector<QVector<int> >& GameArea::getVector() const{
return m_cells;
}
I don't know where the problem is , it's possible to exchange this QVectors via QDataStream andQTcpSockets but after some time after relogging both to the QTcpServer,the size of the QDataStream just increased and is increasing,i don't know is it a memory leak or a problem with the QVector and writing it into the QDataStream.It's possible to play against another without any problems but after ending the session and relogging to the QTcpServer this failure occures.
It's a new session,in a new thread of the server.
With best regards Mark
This is the Function where i send my QVector to the Server.
void Client::sendPlayground(){
// QByteArray wird definiert
QByteArray block;
// QDataStream zum Schreiben in das QByteArray
QDataStream out(&block, QIODevice::WriteOnly);
// Version des DataStereams setzen
out.setVersion(QDataStream::Qt_4_5);
// Daten in den Stream schreiben
out << qint16(0)
<< qint8('P')
<< *m_playground;
// Zurueckspulen und
out.device()->seek(0);
// Datenlaenge in Byte schreiben
qint16 blocksize=qint16(block.size() - sizeof(qint16));
qDebug() << "Size of mine Playground " << sizeof(*m_playground);
qDebug() << "Size of Vector 1"<< sizeof(m_playground->m_defArea->getVector());
qDebug() << "Size of Vector 1"<< sizeof(m_playground->m_offArea->getVector());
qDebug() << "Blocksize Playground Client" << blocksize << "Size of DataStream" << block.size();
out << blocksize;
// QByteArray an den Client schicken
m_serverConnection->write(block);
m_serverConnection->flush();
}
Here is my function for overloading my object called Playground , Playground inherits two Objects called GameArea also stores one QVectors for saving some stats.
Serialize
QDataStream& operator <<(QDataStream & os,const Playground& pg ){
os << pg.m_defArea->getSize() << pg.m_defArea->getVector() << pg.m_offArea->getSize() << pg.m_offArea->getVector();
return os;
}
//Deserialize
QDataStream& operator>>(QDataStream & is,Playground& pg ){
QVector<QVector<int> > vec1;
QSize size1;
QVector<QVector<int> > vec2;
QSize size2;
is >> size1 >> vec2 >> size2 >> vec1;
pg.m_offArea->setSize(size1);
pg.m_offArea->setVector(vec1);
qDebug() << "Size 1" << size1;
qDebug() << "Size 2" << size2;
pg.m_defArea->setSize(size1);
pg.m_defArea->setVector(vec2);
return is;
}
and here is my GameArea , this Objects stores a QVector<QVector<int> > m_cells which saves some stats.
#ifndef GAMEAREA_H
#define GAMEAREA_H
#include <QWidget>
#include <vector>
#include <QMouseEvent>
#include "Game.h"
#include <QDebug>
#include <QVector>
#include <QMap>
#include <QObject>
class Client;
class GameArea : public QWidget
{
Q_OBJECT
public:
GameArea(Client* client);
~GameArea();
//KopyConstructor
GameArea(const GameArea &a);
GameArea& operator=(const GameArea&a);
void setCellState(int x,int y, int value);
int getCellState(int x, int y);
bool isCellEmpty(int x, int y);
void setlocked(bool flag);
void clear();
void setShipMode(Shiptype type);
Shiptype getShipMode();
bool createShip(int x, int y);
void loadResources();
//Returns the stored Vector
const QVector<QVector<int> >& getVector() const;
//Returns the size of the Vector
QSize getSize() const;
//sets the new Vector
void setVector(QVector<QVector<int> > vec);
//sets new Size
void setSize(const QSize& size);
void count_ships();//Hits and Fails in the GameArea for Enemy and You
//Get my count ships
QMap<char,int> * get_m_c_ships();
//Get all maximum of all Ships
QMap<char,int> * get_m_m_ships();
//Get my hits and fails
QMap<char,int> * get_m_hit_fails();
//Get hits and fails of the enemy
QMap<char,int> * get_e_hit_fails();
bool isReady();
signals:
void new_Move();
void won();
void lost();
protected:
virtual void paintEvent(QPaintEvent *e);
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
void getRectForCell(int x, int y, QRect& rect);
private:
QPoint m_tagged;
int m_width;
int m_height;
int m_offsetX; // width-offset from margin
int m_offsetY; // height-offset from margin
QVector<QVector<int> > m_cells;
void reserve_m_cells(int rows,int col);
void resize_m_cells(int rows,int col);
bool isInitialized;
bool m_locked;
Client* m_client;
Shiptype m_shipType;
QImage* image;
QMap<char,int> m_c_ships;
QMap<char,int> m_m_ships;
QMap<char,int> m_hit_fails;
QMap<char,int> e_hit_fails;
};
Q_DECLARE_TYPEINFO(GameArea, Q_MOVABLE_TYPE );
Q_DECLARE_TYPEINFO(QVector<QVector<int> >, Q_MOVABLE_TYPE );
#endif // GAMEAREA_H
Here is my function to get this QVector from my Object
const QVector<QVector<int> >& GameArea::getVector() const{
return m_cells;
}
I don't know where the problem is , it's possible to exchange this QVectors via QDataStream andQTcpSockets but after some time after relogging both to the QTcpServer,the size of the QDataStream just increased and is increasing,i don't know is it a memory leak or a problem with the QVector and writing it into the QDataStream.It's possible to play against another without any problems but after ending the session and relogging to the QTcpServer this failure occures.
It's a new session,in a new thread of the server.
With best regards Mark