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
