Index: code/trunk/src/network/packet/Acknowledgement.h
===================================================================
--- code/trunk/src/network/packet/Acknowledgement.h	(revision 1907)
+++ code/trunk/src/network/packet/Acknowledgement.h	(revision 2087)
@@ -29,4 +29,5 @@
 #define NETWORKACKNOLEDGEMENT_H
 
+#include "../NetworkPrereqs.h"
 #include "Packet.h"
 
@@ -35,7 +36,7 @@
 namespace packet {
 /**
-	@author 
+	@author
 */
-class Acknowledgement : public Packet
+class _NetworkExport Acknowledgement : public Packet
 {
 public:
@@ -43,8 +44,8 @@
   Acknowledgement( uint8_t* data, unsigned int clientID );
   ~Acknowledgement();
-  
+
   inline unsigned int getSize() const;
   bool process();
-  
+
   unsigned int getAckID();
 private:
Index: code/trunk/src/network/packet/Chat.h
===================================================================
--- code/trunk/src/network/packet/Chat.h	(revision 1907)
+++ code/trunk/src/network/packet/Chat.h	(revision 2087)
@@ -2,4 +2,6 @@
 #ifndef NETWORKCHAT_H
 #define NETWORKCHAT_H
+
+#include "../NetworkPrereqs.h"
 
 #include <string>
@@ -11,7 +13,7 @@
 namespace packet {
 /**
-	@author 
+	@author
 */
-class Chat : public Packet
+class _NetworkExport Chat : public Packet
 {
 public:
@@ -19,8 +21,8 @@
   Chat( uint8_t* data, unsigned int clientID );
   ~Chat();
-  
+
   inline unsigned int getSize() const;
   bool process();
-  
+
   unsigned int getMessageLength(){ return messageLength_; };
   unsigned char *getMessage();
Index: code/trunk/src/network/packet/ClassID.h
===================================================================
--- code/trunk/src/network/packet/ClassID.h	(revision 1907)
+++ code/trunk/src/network/packet/ClassID.h	(revision 2087)
@@ -29,4 +29,6 @@
 #define NETWORKCLASSID_H
 
+#include "../NetworkPrereqs.h"
+
 #include <string>
 
@@ -37,7 +39,7 @@
 
 /**
-	@author 
+	@author
 */
-class ClassID : public Packet
+class _NetworkExport ClassID : public Packet
 {
 public:
@@ -45,8 +47,8 @@
   ClassID( uint8_t* data, unsigned int clientID );
   ~ClassID();
-  
+
   inline unsigned int getSize() const;
   bool process();
-  
+
   unsigned int getClassID();
   unsigned int getClassNameLength(){ return classNameLength_; }
Index: code/trunk/src/network/packet/DeleteObjects.h
===================================================================
--- code/trunk/src/network/packet/DeleteObjects.h	(revision 1907)
+++ code/trunk/src/network/packet/DeleteObjects.h	(revision 2087)
@@ -29,4 +29,6 @@
 #define NETWORKPACKETDELETEOBJECTS_H
 
+#include "../NetworkPrereqs.h"
+
 #include "Packet.h"
 
@@ -35,7 +37,7 @@
 namespace packet {
 /**
-	@author 
+	@author
 */
-class DeleteObjects : public Packet
+class _NetworkExport DeleteObjects : public Packet
 {
 public:
@@ -43,10 +45,10 @@
   DeleteObjects( uint8_t* data, unsigned int clientID );
   ~DeleteObjects();
-  
+
   bool fetchIDs();
-  
+
   inline unsigned int getSize() const;
   bool process();
-  
+
 private:
 };
Index: code/trunk/src/network/packet/Gamestate.cc
===================================================================
--- code/trunk/src/network/packet/Gamestate.cc	(revision 1907)
+++ code/trunk/src/network/packet/Gamestate.cc	(revision 2087)
@@ -46,7 +46,7 @@
 #define HEADER GAMESTATE_HEADER(data_)
 
-  
+
 #define PACKET_FLAG_GAMESTATE  ENET_PACKET_FLAG_RELIABLE
-  
+
 Gamestate::Gamestate()
 {
@@ -85,5 +85,5 @@
     return false;
   }
-  
+
 #ifndef NDEBUG
   std::list<Synchronisable*> slist;
@@ -115,5 +115,5 @@
     slist.push_back(*it);
 #endif
-    
+
     //if(it->doSelection(id))
     dataMap_[mem-data_]=(*it);  // save the mem location of the synchronisable data
@@ -157,8 +157,5 @@
     if(!s)
     {
-      s = Synchronisable::fabricate(mem, mode);
-      assert(s);
-//       if(!s)
-//         return false;
+      Synchronisable::fabricate(mem, mode);
     }
     else
@@ -257,5 +254,5 @@
   HEADER->compressed = true;
   assert(HEADER->compressed);
-  COUT(3) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
+  COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl;
   return true;
 }
@@ -264,9 +261,9 @@
   assert(HEADER);
   assert(HEADER->compressed);
-  COUT(3) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
+  COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl;
   unsigned int datasize = HEADER->datasize;
   unsigned int compsize = HEADER->compsize;
   unsigned int bufsize;
-  assert(compsize<=datasize);
+//  assert(compsize<=datasize);
   bufsize = datasize;
   assert(bufsize!=0);
@@ -289,6 +286,16 @@
   //copy over the header
   *GAMESTATE_HEADER(ndata) = *HEADER;
-  //delete old (compressed data)
-  delete[] data_;
+
+  if (this->bDataENetAllocated_){
+    // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will
+    // deallocated it anyway. So data and packet stay together.
+    this->bDataENetAllocated_ = false;
+  }
+  else{
+    // We allocated the memory in the first place (unlikely). So we destroy the old data
+    // and overwrite it with the new decompressed data.
+    delete[] this->data_;
+  }
+
   //set new pointers
   data_ = ndata;
@@ -339,5 +346,5 @@
   assert(data_);
   std::map<unsigned int, Synchronisable *>::iterator it;
-  
+
   // allocate memory for new data
   uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)];
@@ -346,11 +353,11 @@
   uint8_t *newdata = gdata + sizeof(GamestateHeader);
   uint8_t *origdata = GAMESTATE_START(data_);
-  
+
   //copy the GamestateHeader
   *(GamestateHeader*)gdata = *HEADER;
-  
+
   synchronisableHeader *oldobjectheader, *newobjectheader;
   unsigned int objectOffset;
-  
+
   //copy in the zeros
   for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
@@ -360,5 +367,5 @@
     assert(it->second->objectID==oldobjectheader->objectID);
     *newobjectheader = *oldobjectheader;
-    objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableDate variables in the objectheader
+    objectOffset=sizeof(uint8_t)+sizeof(bool); //skip the size and the availableData variables in the objectheader
     if(it->second->doSelection(HEADER->id)){
       newobjectheader->dataAvailable=true; //TODO: probably not neccessary
@@ -390,18 +397,18 @@
   assert(!HEADER->compressed);
   assert(!HEADER->diffed);
-  
+
   //preparations
   std::map<unsigned int, Synchronisable *>::iterator it;
   uint8_t *origdata, *basedata, *destdata, *ndata;
   unsigned int objectOffset, streamOffset=0;    //data offset
-  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; 
+  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
   synchronisableHeader *origheader;
   synchronisableHeader *destheader;
-  
+
   origdata = GAMESTATE_START(this->data_);
   basedata = GAMESTATE_START(base->data_);
   ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
   destdata = ndata + sizeof(GamestateHeader);
-  
+
   // do the diff
   for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
@@ -410,5 +417,5 @@
     origheader = (synchronisableHeader *)(origdata+streamOffset);
     destheader = (synchronisableHeader *)(destdata+streamOffset);
-    
+
     //copy and partially diff the object header
     assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
@@ -420,12 +427,12 @@
     }else{
       *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0;
-      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; 
+      *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0;
     }
     objectOffset=sizeof(synchronisableHeader);
     streamOffset+=sizeof(synchronisableHeader);
-    
+
     //now handle the object data or fill with zeros
     while(objectOffset<origheader->size ){
-      
+
       if(sendData && streamOffset<minsize)
         *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
@@ -434,5 +441,5 @@
       else
         *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
-      
+
       objectOffset++;
       streamOffset++;
@@ -442,5 +449,5 @@
     basedata+=objectOffset;
   }
-  
+
   //copy over the gamestate header and set the diffed flag
   *(GamestateHeader *)ndata = *HEADER; //copy over the header
@@ -458,18 +465,18 @@
   assert(!HEADER->compressed);
   assert(HEADER->diffed);
-  
+
   //preparations
   std::map<unsigned int, Synchronisable *>::iterator it;
   uint8_t *origdata, *basedata, *destdata, *ndata;
   unsigned int objectOffset, streamOffset=0;    //data offset
-  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; 
+  unsigned int minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize;
   synchronisableHeader *origheader;
   synchronisableHeader *destheader;
-  
+
   origdata = GAMESTATE_START(this->data_);
   basedata = GAMESTATE_START(base->data_);
   ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)];
   destdata = ndata + sizeof(GamestateHeader);
-  
+
   // do the undiff
   for(it=dataMap_.begin(); it!=dataMap_.end(); it++){
@@ -478,5 +485,5 @@
     destheader = (synchronisableHeader *)(destdata+streamOffset);
     bool sendData;
-    
+
     //copy and partially diff the object header
     assert(sizeof(synchronisableHeader)==3*sizeof(unsigned int)+sizeof(bool));
@@ -489,12 +496,12 @@
     }else{
       *(unsigned int*)(destdata+sizeof(unsigned int)+sizeof(bool)) = 0;
-      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0; 
+      *(unsigned int*)(destdata+2*sizeof(unsigned int)+sizeof(bool)) = 0;
     }
     objectOffset=sizeof(synchronisableHeader);
     streamOffset+=sizeof(synchronisableHeader);
-    
+
     //now handle the object data or fill with zeros
     while(objectOffset<origheader->size ){
-      
+
       if(sendData && streamOffset<minsize)
         *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor
@@ -503,5 +510,5 @@
       else
         *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered
-      
+
       objectOffset++;
       streamOffset++;
@@ -511,5 +518,5 @@
     basedata+=objectOffset;
   }
-  
+
   //copy over the gamestate header and set the diffed flag
   *(GamestateHeader *)ndata = *HEADER; //copy over the header
Index: code/trunk/src/network/packet/Gamestate.h
===================================================================
--- code/trunk/src/network/packet/Gamestate.h	(revision 1907)
+++ code/trunk/src/network/packet/Gamestate.h	(revision 2087)
@@ -27,4 +27,10 @@
  */
 
+
+#ifndef NETWORK_PACKETGAMESTATE_H
+#define NETWORK_PACKETGAMESTATE_H
+
+#include "../NetworkPrereqs.h"
+
 #include "Packet.h"
 #include "network/Synchronisable.h"
@@ -34,13 +40,9 @@
 #endif
 
-
-#ifndef NETWORK_PACKETGAMESTATE_H
-#define NETWORK_PACKETGAMESTATE_H
-
 namespace network {
 
 namespace packet {
 
-struct GamestateHeader{
+struct _NetworkExport GamestateHeader{
   ENUM::Type packetType;
   int32_t id; // id of the gamestate
@@ -59,5 +61,5 @@
 	@author Oliver Scheuss
 */
-class Gamestate: public Packet{
+class _NetworkExport Gamestate: public Packet{
   public:
     Gamestate();
Index: code/trunk/src/network/packet/Packet.cc
===================================================================
--- code/trunk/src/network/packet/Packet.cc	(revision 1907)
+++ code/trunk/src/network/packet/Packet.cc	(revision 2087)
@@ -49,10 +49,10 @@
 
 namespace packet{
-  
+
 #define PACKET_FLAG_DEFAULT ENET_PACKET_FLAG_NO_ALLOCATE
 #define _PACKETID           0
-  
+
 std::map<ENetPacket *, Packet *> Packet::packetMap_;
-  
+
 Packet::Packet(){
   flags_ = PACKET_FLAG_DEFAULT;
@@ -61,4 +61,5 @@
   data_=0;
   enetPacket_=0;
+  bDataENetAllocated_ = false;
 }
 
@@ -73,4 +74,5 @@
   data_=data;
   enetPacket_=0;
+  bDataENetAllocated_ = false;
 }
 
@@ -86,13 +88,32 @@
   }else
     data_=0;
-}
-
+  bDataENetAllocated_ = p.bDataENetAllocated_;
+}
+
+/**
+@brief
+    Destroys a packet completely.
+    
+    That also means destroying the ENetPacket if one exists. There 
+*/
 Packet::~Packet(){
-  if(enetPacket_){
-    assert(enetPacket_->freeCallback==0);
+  // Deallocate data_ memory if necessary.
+  if (this->bDataENetAllocated_){
+    // In this case ENet allocated data_ and will destroy it.
+  }
+  else if (this->data_) {
+    // This destructor was probably called as a consequence to ENet executing our callback.
+    // It simply serves us to be able to deallocate the packet content (data_) ourselves since
+    // we have created it in the first place.
+    delete[] this->data_;
+  }
+
+  // Destroy the ENetPacket if necessary.
+  // Note: For the case ENet used the callback to destroy the packet, we have already set
+  // enetPacket_ to NULL to avoid destroying it again.
+  if (this->enetPacket_){
+    // enetPacket_->data gets destroyed too by ENet if it was allocated by it.
     enet_packet_destroy(enetPacket_);
   }
-  if(data_)
-    delete[] data_;
 }
 
@@ -107,10 +128,13 @@
       return false;
     }
+    // We deliver ENet the data address so that it doesn't memcpy everything again.
+    // --> We have to delete data_ ourselves!
     enetPacket_ = enet_packet_create(getData(), getSize(), getFlags());
     enetPacket_->freeCallback = &Packet::deletePacket;
-//     enetPacket_->freeCallback = &blub;
+    // Add the packet to a global list so we can access it again once enet calls our
+    // deletePacket method. We can of course only give a one argument function to the ENet C library.
     packetMap_[enetPacket_] = this;
   }
-#ifndef NDEBUG 
+#ifndef NDEBUG
   switch( *(ENUM::Type *)(data_ + _PACKETID) )
   {
@@ -127,7 +151,7 @@
   }
 #endif
-  ENetPacket *temp = enetPacket_;
-  enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
-  network::Host::addPacket( temp, clientID_);
+//  ENetPacket *temp = enetPacket_;
+//  enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
+  network::Host::addPacket( enetPacket_, clientID_);
   return true;
 }
@@ -135,4 +159,5 @@
 Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){
   uint8_t *data = packet->data;
+  assert(ClientInformation::findClient(&peer->address)->getID() != (unsigned int)-2 || !Host::isServer());
   unsigned int clientID = ClientInformation::findClient(&peer->address)->getID();
   Packet *p;
@@ -169,11 +194,25 @@
       break;
   }
+
+  // Data was created by ENet
+  p->bDataENetAllocated_ = true;
+
   return p;
 }
 
-void Packet::deletePacket(ENetPacket *packet){
-  assert(packetMap_[packet]);
-  assert(packetMap_[packet]->enetPacket_==0);
-  delete packetMap_[packet];
+/**
+@brief
+    ENet calls this method whenever it wants to destroy a packet that contains
+    data we allocated ourselves.
+*/
+void Packet::deletePacket(ENetPacket *enetPacket){
+  // Get our Packet from a gloabal map with all Packets created in the send() method of Packet.
+  std::map<ENetPacket*, Packet*>::iterator it = packetMap_.find(enetPacket);
+  assert(it != packetMap_.end());
+  // Make sure we don't delete it again in the destructor
+  it->second->enetPacket_ = 0;
+  delete it->second;
+  //packetMap_.erase(it);
+  COUT(4) << "PacketMap size: " << packetMap_.size() << std::endl;
 }
 
Index: code/trunk/src/network/packet/Packet.h
===================================================================
--- code/trunk/src/network/packet/Packet.h	(revision 1907)
+++ code/trunk/src/network/packet/Packet.h	(revision 2087)
@@ -29,4 +29,6 @@
 #define NETWORKPACKET_H
 
+#include "../NetworkPrereqs.h"
+
 #include <map>
 #include <enet/enet.h>
@@ -37,5 +39,5 @@
 
 namespace packet{
-  
+
 namespace ENUM{
   enum Direction{
@@ -53,9 +55,9 @@
   };
 }
-  
+
 /**
 	@author Oliver Scheuss <scheusso [at] ee.ethz.ch>
 */
-class Packet{
+class _NetworkExport Packet{
   public:
     Packet(const Packet &p);
@@ -63,5 +65,5 @@
     static Packet *createPacket(ENetPacket *packet, ENetPeer *peer);
     static void deletePacket(ENetPacket *packet);
-    
+
     virtual unsigned char *getData(){ return data_; };
     virtual unsigned int getSize() const =0;
@@ -73,5 +75,5 @@
     void setClientID( int id )
       { clientID_ = id; }
-    
+
     bool send();
   protected:
@@ -82,5 +84,11 @@
     unsigned int clientID_;
     ENUM::Direction packetDirection_;
+    /** Pointer to the data. Be careful when deleting it because it might
+        point to a location that was allocated by ENet.
+        See bDataENetAllocated_ */
     uint8_t *data_;
+    /** Tells whether data_ was allocated by ENet or ourselves.
+        data_ might no correlate with enetPacket_->data. */
+    bool bDataENetAllocated_;
   private:
     static std::map<ENetPacket *, Packet *> packetMap_;
Index: code/trunk/src/network/packet/Welcome.h
===================================================================
--- code/trunk/src/network/packet/Welcome.h	(revision 1907)
+++ code/trunk/src/network/packet/Welcome.h	(revision 2087)
@@ -29,4 +29,6 @@
 #define NETWORKWELCOME_H
 
+#include "../NetworkPrereqs.h"
+
 #include "Packet.h"
 
@@ -35,7 +37,7 @@
 
 /**
-	@author 
+	@author
 */
-class Welcome : public Packet
+class _NetworkExport Welcome : public Packet
 {
 public:
@@ -43,9 +45,9 @@
   Welcome( uint8_t* data, unsigned int clientID );
   virtual ~Welcome();
-  
+
   uint8_t *getData();
   inline unsigned int getSize() const;
   bool process();
-  
+
 private:
 };
