[Orxonox-commit 3192] r7885 - code/trunk/src/modules/pong

landauf at orxonox.net landauf at orxonox.net
Mon Feb 14 15:27:57 CET 2011


Author: landauf
Date: 2011-02-14 15:27:56 +0100 (Mon, 14 Feb 2011)
New Revision: 7885

Modified:
   code/trunk/src/modules/pong/PongBall.cc
   code/trunk/src/modules/pong/PongBall.h
Log:
fixed minor memory leak

Modified: code/trunk/src/modules/pong/PongBall.cc
===================================================================
--- code/trunk/src/modules/pong/PongBall.cc	2011-02-14 11:18:12 UTC (rev 7884)
+++ code/trunk/src/modules/pong/PongBall.cc	2011-02-14 14:27:56 UTC (rev 7885)
@@ -47,6 +47,7 @@
         this->speed_ = 0;
         this->accelerationFactor_ = 1.0f;
         this->bat_ = 0;
+        this->bDeleteBats_ = false;
         this->batID_ = new unsigned int[2];
         this->batID_[0] = OBJECTID_UNKNOWN;
         this->batID_[1] = OBJECTID_UNKNOWN;
@@ -57,6 +58,13 @@
 
     PongBall::~PongBall()
     {
+        if (this->isInitialized())
+        {
+            if (this->bDeleteBats_)
+                delete[] this->bat_;
+
+            delete[] this->batID_;
+        }
     }
 
     void PongBall::registerVariables()
@@ -166,6 +174,12 @@
 
     void PongBall::setBats(WeakPtr<PongBat>* bats)
     {
+        if (this->bDeleteBats_)
+        {
+            delete[] this->bat_;
+            this->bDeleteBats_ = false;
+        }
+
         this->bat_ = bats;
         this->batID_[0] = this->bat_[0]->getObjectID();
         this->batID_[1] = this->bat_[1]->getObjectID();
@@ -174,7 +188,15 @@
     void PongBall::applyBats()
     {
         if (!this->bat_)
-            this->bat_ = new WeakPtr<PongBat>[2]; // TODO: delete this somewhere
+        {
+            if (this->bDeleteBats_)
+                delete[] this->bat_;
+            else
+                this->bDeleteBats_ = true;
+
+            this->bat_ = new WeakPtr<PongBat>[2];
+        }
+
         if (this->batID_[0] != OBJECTID_UNKNOWN)
             this->bat_[0] = orxonox_cast<PongBat*>(Synchronisable::getSynchronisable(this->batID_[0]));
         if (this->batID_[1] != OBJECTID_UNKNOWN)

Modified: code/trunk/src/modules/pong/PongBall.h
===================================================================
--- code/trunk/src/modules/pong/PongBall.h	2011-02-14 11:18:12 UTC (rev 7884)
+++ code/trunk/src/modules/pong/PongBall.h	2011-02-14 14:27:56 UTC (rev 7885)
@@ -79,6 +79,7 @@
             float accelerationFactor_;
             float batlength_;
             WeakPtr<PongBat>* bat_;
+            bool bDeleteBats_;
             unsigned int* batID_;
             float relMercyOffset_;
     };




More information about the Orxonox-commit mailing list